Recipe 20.4. Reading Configuration Variables


20.4.1. Problem

You want to get the value of a PHP configuration setting.

20.4.2. Solution

Use ini_get( ):

// find out the include path: $include_path = ini_get('include_path');

20.4.3. Discussion

To get all the configuration variable values in one step, call ini_get_all( ). It returns the variables in an associative array, and each array element is itself an associative array. The second array has three elements: a global value for the setting, a local value, and an access code:

// put all configuration variables in an associative array $vars = ini_get_all(); print_r($vars['include_path']); Array (     [global_value] => .:/usr/local/lib/php/     [local_value] => .:/usr/local/lib/php/     [access] => 7 )

The global_value is the value set from the php.ini file; the local_value is adjusted to account for any changes made in the web server's configuration file, any relevant .htaccess file s, and the current script. The value of access is a numeric constant representing the places where this value can be altered. Table 20-1 explains the values for access. Note that the name access is a little misleading in this respect, as the value of the setting can always be checked, but not always adjusted.

Table 20-1. Access values

Value

PHP constant

Meaning

1

PHP_INI_USER

Any script, using ini_set( )

2

PHP_INI_PERDIR

Directory level, using .htaccess

4

PHP_INI_SYSTEM

System level, using php.ini or httpd.conf

7

PHP_INI_ALL

Everywhere: scripts, directories, and the system


A value of 6 means the setting can be changed in both the directory and system level, as 2 + 4 = 6. In practice, there are no variables modifiable only in PHP_INI_USER or PHP_INI_PERDIR, and all variables are modifiable in PHP_INI_SYSTEM, so everything has a value of 4, 6, or 7.

You can also get variables belonging to a specific extension by passing the extension name to ini_get_all( ):

// return just the session module specific variables $session = ini_get_all('session');

By convention, the variables for an extension are prefixed with the extension name and a period. So all the session variables begin with session. and all the Java variables begin with java., for example.

Since ini_get( ) returns the current value for a configuration directive, if you want to check the original value from the php.ini file, use get_cfg_var( ):

$original = get_cfg_var('sendmail_from'); // have we changed our address?

The value returned by get_cfg_var( ) is the same as what appears in the global_value element of the array returned by ini_get_all( ).

20.4.4. See Also

Recipe 20.5 on setting configuration variables; documentation on ini_get( ) at http://www.php.net/ini-get, ini_get_all( ) at http://www.php.net/ini-get-all, and get_cfg_var( ) at http://www.php.net/get-cfg-var; a complete list of configuration variables, their defaults, and when they can be modified at http://www.php.net/manual/en/ini.php. ch20_configuration




PHP Cookbook, 2nd Edition
PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net