Several configuration file functions in php

Source: Internet
Author: User
Php configuration functions are several ini _ * functions, mainly for configuration file operations. In fact, there are four functions: ini_get, ini_set, ini_get_all, and ini_restore. The most useful ones are ini_set and ini_get. * Ini_get (): gets the option value of the configuration file. This function can be used by many people, that is, several configuration functions are used to obtain the configuration file php. Ini _*Mainly for configuration file operations, there are actually four functions: Ini_get, Ini_set, Ini_get_all, Ini_restore. What is most useful in my personal experience is Ini_setAnd Ini_get.

* Ini_get (): Get the option value of the configuration file

This function is intended to be used by many people to obtain the value of an option in the configuration file.TrueReturns the value.1, If yesFalseReturns the value.0, Returns the string.
For example, in the manual:
/*
Our php. ini contains the following settings:

Display_errors = On
Register_globals = Off
Post_max_size = 8 M
*/
Echo 'display _ errors = '. ini_get ('display _ errors'). "\ n ";//
Show whether the error is enabled
Echo 'register _ globals = '. ini_get ('register _ globals'). "\ n ";//
Whether the global variable is enabled
Echo 'post _ max_size = '. ini_get ('post _ max_size'). "\ n ";//
Maximum file size 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

This function is mainly used to obtain the configuration file and facilitate many operations. For example, you want to filter strings, but it is unclear.Magic_quotes_gpcYou can write a function as follows:

/*String filter function*/
Function stringFilter ($ str)
{
If (ini_get ('Magic _ quotes_gpc )'){
Return $ str;
} Else {
Return addslashes ($ str );
}
}
Of course, if you cannot know whether your global variables are enabled, you can also customize the following functions:

/*Variable detection function*/
Function getGetVar ($ var)
{
If (ini_set ('register _ gobals ')){
Return $ var;
} Else {
Return $ _ GET ['var'];
}
}
Of course, you can do a lot of things for yourself.


* Ini_set
Function: setPhp. iniSome variable values in

This function is the value in the setting option. it takes effect after the function is executed. when the script ends, this setting also becomes invalid. Not all options can be changed to function settings. You can set specific values to view the list in the manual.
Is to be able to setPhp. iniFor example,Display_errorIf you want to display the error information in the program so that you can debug the program, you can use this function:

Ini_set ("display_errors", "On ");

The program on your page will display the error message, and you can also useError_reportingTo set the displayed error information level.
If you need to increase the script execution time, you can set:

Ini_set ("maid", "180 ");

The script execution time is determined by the default30Seconds180Of course, you can also useSet_time_limit ().
In factIni_setAndIni_getIf combined, it is very good. For example, if you want to add your own include file path to the configuration file, but do you have the permission to change it?Php. iniThen you can combine two functions:

Ini_set ('include _ path', ini_get ('include _ path'). ':/your_include_dir :');


* Ini_get_all:
Get all set option variables

Returns all option values in the form of an array, so that you can easilyPhpinfo ()It can be used when it is unavailable.

Example of a manual, for example:

$ 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:Reply to the default value of the configuration file

Is to reply to the default value of the configuration file, when you useIni_setYou can use this configuration to restore it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.