The most common ini function analysis in PHP is for the php.ini configuration file

  • 2020-03-31 20:35:20
  • OfStack

* ini_get() : gets the option value for the configuration file

This function, which I'm sure many of you have done before, takes the value of an option in a configuration file, returns 1 if it's true, 0 if it's false, and returns a string.

For example, the example in the manual:


Echo 'display_errors = '.ini_get ('display_errors'). "\n"; // shows whether the error is open or not
Echo 'register_globals = '.ini_get ('register_globals'). "\n"; // global variable is open
Echo 'post_max_size ='. Ini_get (' post_max_size '). "\ n"; // maximum size of file that can be submitted
Echo 'post_max_size + 1 ='. (ini_get (' post_max_size) + 1). "\ n";
? >

Output:

Display_errors = 1
Register_globals = 0
Post_max_size = 8 m
Post_max_size + 1 = 9

The main purpose of this function is to obtain the configuration file, can be convenient for you to do a lot of things. For example, if you want to do string filtering, but it is not clear whether magic_quotes_gpc is open or not, you can write a function like this:


The function stringFilter ($STR)
{
If (ini_get (' magic_quotes_gpc)) {
Return $STR;
} else {
Return addslashes ($STR);
}
}

Of course, if you don't know whether your global variable is open or not, you can also customize functions like this:


The function getGetVar ($var)
{
If (ini_set (' register_gobals)) {
Return the $var.
} else {
Return the $_GET [' var '];
}
}

Of course, there are a lot of USES you can make that you can enjoy.

* ini_set function: sets the value of certain variables in php.ini

This function is the value in the Settings option, which takes effect after the function is executed, and also expires at the end of the script. Not all options can be set by the change function. Specific values can be set, you can see the list in the manual.

For example, the display_error option is off, but you want to display the error message in the program, so that you can debug the program, then you can use this function:

Ini_set (" display_errors ", "On");

You can use error_reporting to set the level of the error message displayed.

If you need to increase the script execution time, you can set:

Ini_set (" max_execution_time ", "180");

The default execution time of the script will be 180 seconds instead of 30 seconds. Of course, you can also use set_time_limit().

In fact, when you combine ini_set with ini_get, it's pretty good. For example, if you want to add your own include file path to the configuration file, but you don't have permission to change php.ini, then you can combine two functions:

Ini_set (' include_path, ini_get (' include_path '). '/ your_include_dir:');

* ini_get_all: gets all the Settings options variables

Return all the options as an array, so you can use them when phpinfo() is not available.

Manual examples, such as:

$inis = ini_get_all ();

Print_r ($inis);

? >

Partial output:

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: returns the default value of the configuration file

Returns the default value of the configuration file, which you can use to restore when using the ini_set setting.


Related articles: