Thursday, February 11, 2010

How to get and set the values of php.ini files through your php script

As we know that every php installation contains a configuration file named php.ini. This file contains all the required values that is needed to run and execute your php script. It sometimes happen that the configuration file that resides in the server(could be your local server or remote server) may conflict on some issues with your php script and in that case you may want to check/append the values so that your script works perfectly. You can do this with the help of two php built in function called "ini_get" and "ini_set".

Many of you may have used these two functions but i want to give a clear idea so that anyone can get help about using it regardless of knowing this before. ini_get function is used to get the value of a specific configuration variable from the php.ini file. This can be used so that you can know the default value of your desired configuration variable. The syntax of ini_get function is:

string ini_get ( string $varname );
?>
Here the $varname is your configuration variable/option name.

Example:

echo ini_get ( 'register_globals' );
?>

This will shows the value for the regiser_globals valus from php.ini which is 0 if its not set.
So using this function, you will get your desired configuration option value from the php.ini files.

To change the value of the configuration option, you can use the ini_set function. The syntax of ini_set function is:

string ini_set ( string $varname , string $newvalue );
?>

Here the $varname is your configuration variable/option name and $newvalus is the value that you want to set for the option.


Example:
if (!ini_get('display_errors')) {
    ini_set('display_errors', 1
);
}

In this example, if the ini file doesn't have any error display option, then you can set the value of the error display through the ini_set function.

Please be noted that you can not change all the configuration options with the ini_set functions. There are some options which can not be set from you script. You may change those using .htaccess. You can go to http://www.php.net to look for the list of the options that you can set with ini_set function.

I suggest you study more about these function and practice to get used to of it. Please comment if you have any suggestion. Thank You and hope this helps!!!!!!

No comments: