本文目錄一覽:
- 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/zh-hant/n/150045.html