PHP環境設定 PHP最常用的ini函數

<span =“apple-style-span” style="font-family: verdana, helvetica, arial, sans-serif; font-size: 16px; border-collapse: collapse; color: rgb(68, 68, 68); "><span =“apple-style-span” style=“white-space: nowrap;”>php的環境函數就是幾個ini_的函數,主要是針對環境設定的操作,
其實就四個函數:ini_get、ini_set、ini_get_all、ini_restore。
個人感覺最有用的就是ini_set和ini_get。

<strong style="word-wrap: break-word; text-align: left; font-style: normal; line-height: normal; font-weight: bold; ">
ini_get():獲取環境設定的選項值
這個函數相信很多人都使過,就是獲取環境設定中某一個選項的值,
如果是true值就返回1,如果是false值就返回0,字符串就返回字符串。 

比如手冊中的例子: 

<table cellspacing=“0” =“t_table” style="word-wrap: break-word; empty-cells: show; border-collapse: collapse; line-height: normal; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: rgb(227, 237, 245); border-right-color: rgb(227, 237, 245); border-bottom-color: rgb(227, 237, 245); border-left-color: rgb(227, 237, 245); width: 1036px; table-layout: fixed; margin-left: 1px; "><?php 
/ 
Our php.ini contains the following settings: 

display_errors = On 
register_globals = Off 
post_max_size = 8M 

echo 'display_errors = ’ . ini_get(‘display_errors’) . “\n”; //顯示錯誤是否打開 
echo 'register_globals = ’ . ini_get(‘register_globals’) . “\n”;//全域變數是否打開 
echo ‘post_max_size = ’ . ini_get(‘post_max_size’) . “\n”;//最多能提交的文件大小 
echo ‘post_max_size+1 = ’ . (ini_get(‘post_max_size’)+1) . “\n”; 
?>
輸出: 

<table cellspacing=“0” =“t_table” style="word-wrap: break-word; empty-cells: show; border-collapse: collapse; line-height: normal; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: rgb(227, 237, 245); border-right-color: rgb(227, 237, 245); border-bottom-color: rgb(227, 237, 245); border-left-color: rgb(227, 237, 245); width: 1036px; table-layout: fixed; margin-left: 1px; ">display_errors = 1 
register_globals = 0 
post_max_size = 8M 
post_max_size+1 = 9

這個函數主要是為了獲取環境設定,可以方便你很多操作。
比如你想操作字串過濾,但是又不清楚magic_quotes_gpc有沒有打開,
所以你就可以這樣寫一個函數: 

<table cellspacing=“0” =“t_table” style="word-wrap: break-word; empty-cells: show; border-collapse: collapse; line-height: normal; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: rgb(227, 237, 245); border-right-color: rgb(227, 237, 245); border-bottom-color: rgb(227, 237, 245); border-left-color: rgb(227, 237, 245); width: 1036px; table-layout: fixed; margin-left: 1px; ">/* 字串過濾函數
function stringFilter($str) 

if (ini_get(‘magic_quotes_gpc)’) { 
return $str; 
} else { 
return addslashes($str); 

}
當然,如果你無法知道你的全域變數是否打開,也可以定制這樣的函數: 

<table cellspacing=“0” =“t_table” style="word-wrap: break-word; empty-cells: show; border-collapse: collapse; line-height: normal; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: rgb(227, 237, 245); border-right-color: rgb(227, 237, 245); border-bottom-color: rgb(227, 237, 245); border-left-color: rgb(227, 237, 245); width: 1036px; table-layout: fixed; margin-left: 1px; ">/
變量檢測函數
function getGetVar($var) 

if (ini_set(‘register_gobals’)) { 
return $var; 
} else { 
return $_GET[‘var’]; 

}可以做很多用途,自己慢慢體會。


<strong style="word-wrap: break-word; text-align: left; font-style: normal; line-height: normal; font-weight: bold; ">
ini_set函數:設置php.ini中的某些變數值
這個函數是設置選項中的值,在執行函數後生效,腳本結束的時候,這個設置也失效。
不是所有的選項都能被改函數設置的。實際那些值能夠設置,可以查看php手冊中的列表。

就是能夠設置php.ini中的選項值比如,display_error選項關閉了,但是你要顯示程序中的錯誤信息,方便你調試程序,那麽就可以使用這個函數: 

<table cellspacing=“0” =“t_table” style="word-wrap: break-word; empty-cells: show; border-collapse: collapse; line-height: normal; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: rgb(227, 237, 245); border-right-color: rgb(227, 237, 245); border-bottom-color: rgb(227, 237, 245); border-left-color: rgb(227, 237, 245); width: 1036px; table-layout: fixed; margin-left: 1px; ">ini_set(“display_errors”, “On”);
PHP Time out 時間設定,增加腳本執行時間,那麽可以設置: 
<table cellspacing=“0” =“t_table” style="word-wrap: break-word; empty-cells: show; border-collapse: collapse; line-height: normal; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: rgb(227, 237, 245); border-right-color: rgb(227, 237, 245); border-bottom-color: rgb(227, 237, 245); border-left-color: rgb(227, 237, 245); width: 1036px; table-layout: fixed; margin-left: 1px; ">ini_set(“max_execution_time”, “180”);// 0 為無限制時間
那麽腳本執行時間就由默認的30秒變為180秒,當然,你也可以使用set_time_limit()來設置。

其實你把ini_set和ini_get結合使的話,非常好。
比如你想在環境設定裏添加自己的包含文件路徑,但是你有沒有權限更改php.ini,那麽你可以結合兩個函數:

<table cellspacing=“0” =“t_table” style="word-wrap: break-word; empty-cells: show; border-collapse: collapse; line-height: normal; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: rgb(227, 237, 245); border-right-color: rgb(227, 237, 245); border-bottom-color: rgb(227, 237, 245); border-left-color: rgb(227, 237, 245); width: 1036px; table-layout: fixed; margin-left: 1px; ">ini_set(‘include_path’,ini_get(‘include_path’).’:/your_include_dir:’);

<strong style="word-wrap: break-word; text-align: left; font-style: normal; line-height: normal; font-weight: bold; ">* ini_get_all: 獲取所有的設置選項變數
把所有選項值以陣列的形式返回,方便你當phpinfo()無法使用的時候來使用。 
手冊例子,比如: 

<table cellspacing=“0” =“t_table” style="word-wrap: break-word; empty-cells: show; border-collapse: collapse; line-height: normal; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: rgb(227, 237, 245); border-right-color: rgb(227, 237, 245); border-bottom-color: rgb(227, 237, 245); border-left-color: rgb(227, 237, 245); width: 1036px; table-layout: fixed; margin-left: 1px; "><?php 
$inis = ini_get_all(); 

print_r($inis); 

?>
部分輸出:

<table cellspacing=“0” =“t_table” style="word-wrap: break-word; empty-cells: show; border-collapse: collapse; line-height: normal; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: rgb(227, 237, 245); border-right-color: rgb(227, 237, 245); border-bottom-color: rgb(227, 237, 245); border-left-color: rgb(227, 237, 245); width: 1036px; table-layout: fixed; margin-left: 1px; ">Array 

[allow_call_time_pass_reference] => Array 

[global_value] => 1 
[local_value] => 1 
[access] => 6 

[allow_url_fopen] => Array 

[global_value] => 1 
[local_value] => 1 
[access] => 7 

… 
)

<strong style="word-wrap: break-word; text-align: left; font-style: normal; line-height: normal; font-weight: bold; ">* ini_restore: 回複環境設定默認的值 

就是回複環境設定預設值,當你使用ini_set設置後可以使用它來恢複。