php的几个配置文件函数_PHP
Jun 01, 2016 pm 12:38 PM* ini_get():获取配置文件的选项值
这个函数相信很多人都使过,就是获取配置文件中某一个选项的值,如果是true值就返回1,如果是false值就返回0,字符串就返回字符串。
比如手册中的例子:
/*
Our php.ini contains the following settings:
display_errors = On
register_globals = Off
post_max_size =
*/
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 =
post_max_size+1 = 9
这个函数主要是为了获取配置文件,可以方便你很多操作。比如你想操作字符串过滤,但是又不清楚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'];
}
}
当然,你可以做很多用途,自己慢慢体会。
* ini_set函数:设置php.ini中的某些变量值
这个函数是设置选项中的值,在执行函数后生效,脚本结束的时候,这个设置也失效。不是所有的选项都能被改函数设置的。具体那些值能够设置,可以查看手册中的列表。
就是能够设置php.ini中的选项值比如,display_error选项关闭了,但是你要显示程序中的错误信息,方便你调试程序,那么就可以使用这个函数:
ini_set("display_errors", "On");
那么在你这个页面的程序都会显示错误信息了,而且你还可以使用error_reporting来设置显示的错误信息级别。
如果你需要增加脚本执行时间,那么可以设置:
ini_set("max_execution_time", "180");
那么脚本执行时间就由默认的30秒变为180秒,当然,你也可以使用set_time_limit()来设置。
其实你把ini_set和ini_get结合使的话,非常好。比如你想在配置文件里添加自己的包含文件路径,但是你有没有权限更改php.ini,那么你可以结合两个函数:
ini_set('include_path',ini_get('include_path').':/your_include_dir:');
* ini_get_all: 获取所有的设置选项变量
把所有选项值以数组的形式返回,方便你当phpinfo()无法使用的时候来使用。
手册例子,比如:
$inis = ini_get_all();
print_r($inis);
?>
部分输出:
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
)
...
)
* ini_restore: 回复配置文件默认的值
就是回复配置文件默认的值,当你使用ini_set设置后可以使用它来恢复。

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Do Not Disturb Mode Not Working in iPhone: Fix

Tips for dynamically creating new functions in golang functions

Considerations for parameter order in C++ function naming

How to write efficient and maintainable functions in Java?

Complete collection of excel function formulas

Comparison of the advantages and disadvantages of C++ function default parameters and variable parameters

What are the benefits of C++ functions returning reference types?

What is the difference between custom PHP functions and predefined functions?
