php ini_get函數使用方法詳解

來源:互聯網
上載者:User


例如:php.ini檔案中包含了以下的設定:

 代碼如下 複製代碼

register_globals = Off

post_max_size = 8M

PHP部分我們可以這樣擷取:

 

 代碼如下 複製代碼
<?php
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";
echo 'post_max_size in bytes = ' . return_bytes(ini_get('post_max_size'));
?>

如果想擷取整個php.ini裡的變數值,我們可以用ini_get的加強函數 ini_get_all()

ini_get_all()函數以數組的形式返回整個php的環境變數


當然如果你只是想瞭解下php的配置資訊用
<?php
phpinfo();
?>

接著向下看

php手冊中有一個例子

 

 代碼如下 複製代碼
<?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”; 
?>

輸出:

display_errors = 1
register_globals = 0
post_max_size = 8M
post_max_size+1 = 9

PHP配置函數ini_get()主要是為了擷取設定檔,可以方便你很多操作。比如你想操作字串過濾,但是又不清楚magic_quotes_gpc有沒有開啟,所以你就可以這樣寫一個函數:

 代碼如下 複製代碼

/* 字串過濾函數 */ 

function stringFilter($str) 

if (ini_get(’magic_quotes_gpc)’)

return $str; 
}
else { 
return addslashes($str); 

}

當然,如果你無法知道你的全域變數是否開啟,也可以定製這樣的函數:
/* 變數檢測函數 */ 

 代碼如下 複製代碼
function getGetVar($var) 

if (ini_set(’register_gobals’))

return $var; 
}
else { 
return $_GET['var']; 

}

當然,你可以用PHP配置函數ini_get()做很多用途,自己慢慢體會。

補充

1.ini_get()擷取配置參數,ini_set()設定配置參數

 代碼如下 複製代碼


<?php
echo ini_get('display_errors'); //1
//動態修改php.ini配置資訊,指令碼執行後失效
ini_set('display_errors',0);
echo ini_get('display_errors');//0

2.ini_get_all()擷取所有配置資訊

 代碼如下 複製代碼


<?php
//列印所有配置資訊,巨多。。。
print_r(ini_get_all());

3.ini_restore()恢複配置資訊到原始值

 代碼如下 複製代碼

<?php

echo ini_get('display_errors'); //1
//動態修改php.ini配置資訊,指令碼執行後失效
ini_set('display_errors',0);
ini_restore('display_errors');
echo ini_get('display_errors');//1

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.