本文目录一览:
- 1、config.php 用什么打开这个啊 怎么修改
- 2、thinkphp 5.1 获取不到config的自定义的值
- 3、php 判断用户是否登录
- 4、php 函数运行完了 不就释放了吗,还要等脚本结束
- 5、php中getConfig是怎么执行的?
config.php 用什么打开这个啊 怎么修改
对形如config.php文件的读取,修改等操作的代码,需要的朋友可以参考下
..复制代码 代码如下:
?php
$name=”admin”;//kkkk
$bb=’234′;
$db=4561321;
$kkk=”admin”;
?
函数定义:
配置文件数据值获取:function getconfig($file, $ini, $type=”string”)
配置文件数据项更新:function updateconfig($file, $ini, $value,$type=”string”)
调用方式:
复制代码 代码如下:
getconfig(“./2.php”, “bb”);//
updateconfig(“./2.php”, “kkk”, “admin”);
复制代码 代码如下:
?php
//配置文件数据值获取。
//默认没有第三个参数时,按照字符串读取提取”中或””中的内容
//如果有第三个参数时为int时按照数字int处理。
function getconfig($file, $ini, $type=”string”)
{
if ($type==”int”)
{
$str = file_get_contents($file);
$config = preg_match(“/” . $ini . “=(.*);/”, $str, $res);
Return $res[1];
}
else
{
$str = file_get_contents($file);
$config = preg_match(“/” . $ini . “=\”(.*)\”;/”, $str, $res);
if($res[1]==null)
{
$config = preg_match(“/” . $ini . “='(.*)’;/”, $str, $res);
}
Return $res[1];
}
}
//配置文件数据项更新
//默认没有第四个参数时,按照字符串读取提取”中或””中的内容
//如果有第四个参数时为int时按照数字int处理。
function updateconfig($file, $ini, $value,$type=”string”)
{
$str = file_get_contents($file);
$str2=””;
if($type==”int”)
{
$str2 = preg_replace(“/” . $ini . “=(.*);/”, $ini . “=” . $value . “;”, $str);
}
else
{
$str2 = preg_replace(“/” . $ini . “=(.*);/”, $ini . “=\”” . $value . “\”;”,$str);
}
file_put_contents($file, $str2);
}
//echo getconfig(“./2.php”, “bb”, “string”);
getconfig(“./2.php”, “bb”);//
updateconfig(“./2.php”, “kkk”, “admin”);
//echo “br/”.getconfig(“./2.php”, “name”,”string”);
?
复制代码 代码如下:
//完善改进版
/**
* 配置文件操作(查询了与修改)
* 默认没有第三个参数时,按照字符串读取提取”中或””中的内容
* 如果有第三个参数时为int时按照数字int处理。
*调用demo
$name=”admin”;//kkkk
$bb=’234′;
$bb=getconfig(“./2.php”, “bb”, “string”);
updateconfig(“./2.php”, “name”, “admin”);
*/
function get_config($file, $ini, $type=”string”){
if(!file_exists($file)) return false;
$str = file_get_contents($file);
if ($type==”int”){
$config = preg_match(“/”.preg_quote($ini).”=(.*);/”, $str, $res);
return $res[1];
}
else{
$config = preg_match(“/”.preg_quote($ini).”=\”(.*)\”;/”, $str, $res);
if($res[1]==null){
$config = preg_match(“/”.preg_quote($ini).”='(.*)’;/”, $str, $res);
}
return $res[1];
}
}
function update_config($file, $ini, $value,$type=”string”){
if(!file_exists($file)) return false;
$str = file_get_contents($file);
$str2=””;
if($type==”int”){
$str2 = preg_replace(“/”.preg_quote($ini).”=(.*);/”, $ini.”=”.$value.”;”,$str);
}
else{
$str2 = preg_replace(“/”.preg_quote($ini).”=(.*);/”,$ini.”=\””.$value.”\”;”,$str);
}
file_put_contents($file, $str2);
}
thinkphp 5.1 获取不到config的自定义的值
据我分析应该是文件执行顺序的问题
我猜测你的config::set 是在控制器里面写的,当代码执行到你的config::set的时候文件已经跑完所有的config文件了
当代码跑到你的config::get的时候代码没有重新跑所有的config文件所有就获取不到你的set值了
所以需要你的代码跑到config文件之前进行set操作
欢迎光临我的技术博客 网页链接
php 判断用户是否登录
判断用户登陆主要分几个过程,第一步是用户登陆成功后把数据保存到session中,然后当用户访问需要登陆过的用户权限时就来判断session是否为空,如果不是就登录成功。下面来看个实例判断用户登陆主要分几个过程,第一步是用户登陆成功后把数据保存到session中,然后当用户访问需要登陆过的用户权限时就来判断session是否为空,如果不是就登录成功。下面来看个实例?php教程session_start(); if(getconfig(“chatroom_admin”)==$_post[“username”]getconfig(“chatroom_adminpassword”)==md5($_post[“password”])){//登陆成功,注册sessionsession_register(“administrator”);if(isset($_session[“hack_num”])){session_unregister(“hack_num”);}$_session[“administrator”] = “yes”;header(“location:main.php”);exit; }else{if($_session[“hack_num”]==””){session_register(“hack_num”);$_session[“hack_num”] = 1;}else{$_session[“hack_num”] ++; }header(“location:../notice.php?id=”.admin_login_lost);exit;}?看到红色的没,那里就是用户登录成功把数据保存到session[‘hack_num’]面。?php//这个页面首先判断用户是否正确登录,如未登录,就转到登录页面。
php 函数运行完了 不就释放了吗,还要等脚本结束
首先说下,题主这个问题不是函数释放的问题
问题在于 require_once
第一次:在get_config内 第一次 引入 conf.php 赋值给 $conf , 此时 $conf 得到期望 数组 ,没有任何问题。
(注:此时已经在本次运行环境中引入了conf.php)
第二次:在get_config内再次尝试引入 conf.php 此时 require_once 会检测到运行环境中已经引入了该文件,此时返回值为 true 对 true 进行取值 肯定是 null
题主正确的做法是把 require_once 放入全局GLOBALS内,在 get_config 内引入全局变量即可。
修改后的代码:
a.php
$GLOBALS[‘conf’] = require_once ‘conf.php’;
function get_config($key, $default = null){
return isset($GLOBALS[‘conf’][$key])
? $GLOBALS[‘conf’][$key]
: $default;
}
var_dump(get_Config(‘name’));
var_dump(get_Config(‘age’));
— 共有 1 条评论 —
hphper(注:此时已经在本次运行环境中引入了conf.php) 这句话,这个引入是在函数里引入,函数执行完了后在这个运行环境里有引入吗,如果在这个运行文件里有引入,那么我直接打印 conf.php里的$config应该有值,但是没有 也就是说在全局里并没有引入 那么就只在函数里引入了,为什么第一次的函数执行 会对第二次的执行有影响呢 (3年前)
评论(1)| 引用此答案| 举报 (2014-06-14 13:12)
梁上有木梁上有木 3年前
题主这个config的格式不需要把引入的文件赋值,只需要require_once就可以了
a.php
1 function get_config($key){
2 require_once ‘conf.php’;
3 return $conf[$key];
4 }
5 echo get_Config(‘name’);
6
7 echo get_Config(‘age’);
conf.php
1 $config=array(
2 ‘name’=’hk’,
3 ‘age’=’22’
5 );
评论(0)| 引用此答案| 举报 (2014-06-14 15:34)
hphperhphper 3年前
引用来自“梁上有木”的评论
题主这个config的格式不需要把引入的文件赋值,只需要require_once就可以了
a.php
1 function get_config($key){
2 require_once ‘conf.php’;
3 return $conf[$key];
4 }
5 echo get_Config(‘name’);
6
7 echo get_Config(‘age’);
conf.php
1 $config=array(
2 ‘name’=’hk’,
3 ‘age’=’22’
5 );
function get_config($key){
require_once ‘conf.php’;
return $conf[$key];
}
里$conf是啥,
是a.php里的 $config吧
就算是
第二次调用 也是不行的
评论(0)| 引用此答案| 举报 (2014-06-14 16:46)
TuesdayTuesday 3年前
终于明白为什么php 的排名日落千丈了.
— 共有 1 条评论 —
hphper是我这样的小白 多了是吗? (3年前)
评论(1)| 引用此答案| 举报 (2014-06-14 18:43)
南湖船老大南湖船老大 3年前
各种乱入,各种小白啊,贵圈太乱了。。。
都是想当然地写代码。。。require_once 你赋值干嘛。。。而且你这种写法很粗糙,也很dirty,你造吗?
推荐写法:
$config = call_user_func(function() {
return include ‘config.php’;
});
— 共有 3 条评论 —
南湖船老大回复 @hphper : 变量可能被污染,用匿名函数和闭包就避免了这种情况 (3年前)
hphper为什么粗糙,dirty了,怎么判断粗糙不粗糙呢 (3年前)
hphper哦,看来进步空间还很大啊 (3年前)
评论(3)| 引用此答案| 举报 (2014-06-14 19:46)
hphperhphper 3年前
引用来自“D哥”的评论
首先说下,题主这个问题不是函数释放的问题
问题在于 require_once
第一次:在get_config内 第一次 引入 conf.php 赋值给 $conf , 此时 $conf 得到期望 数组 ,没有任何问题。
(注:此时已经在本次运行环境中引入了conf.php)
第二次:在get_config内再次尝试引入 conf.php 此时 require_once 会检测到运行环境中已经引入了该文件,此时返回值为 true 对 true 进行取值 肯定是 null
题主正确的做法是把 require_once 放入全局GLOBALS内,在 get_config 内引入全局变量即可。
修改后的代码:
a.php
$GLOBALS[‘conf’] = require_once ‘conf.php’;
function get_config($key, $default = null){
return isset($GLOBALS[‘conf’][$key])
? $GLOBALS[‘conf’][$key]
: $default;
}
var_dump(get_Config(‘name’));
var_dump(get_Config(‘age’));
谢谢!,我在函数里用静态变量也可以,您这也是一种方法
我仍不理解的是:第二次 执行时,既然它已经检测到 已经引入了该文件,那我直接用conf.php 里的 $config为啥也不行啊
我最不明白的是 函数里的 require_once 对正在执行这个函数的文件的影响,下次再require_once的时候 为啥受上一次函数的影响
php中getConfig是怎么执行的?
分数太少了,又没有上下文。
表面上看getConfig因该是一个函数
原创文章,作者:MURZ,如若转载,请注明出处:https://www.506064.com/n/150045.html