07
Jan
2009
admin

Customize a PHP configuration file at Runtime

You can change your php.ini file configuration at runtime using PHP scripts. Customize such settings as whether global variables are turned on, the default directory to upload files to when writing upload scripts, and the maximum allowed size for uploaded files.

PHP Options and Information functions enable you to get a lot of information about PHP itself, e.g. runtime configuration, loaded extensions, version and much more. You'll also find functions to set options for your running PHP.

The configuration file
The configuration file (php.ini) is read when PHP starts up. For the server module versions of PHP, this happens only once when the web server is started. For the CGI and CLI version, it happens on every invocation.

php.ini is searched in these locations (in order):

  • SAPI module specific location (PHPIniDir directive in Apache 2, -c command line option in CGI and CLI, php_ini parameter in NSAPI, PHP_INI_PATH environment variable in THTTPD)
  • The PHPRC environment variable. Before PHP 5.2.0 this was checked after the registry key mentioned below.
  • As of PHP 5.2.0, the following registry locations are searched in order: HKEY_LOCAL_MACHINE\SOFTWARE\PHP\x.y.z\IniFilePath, HKEY_LOCAL_MACHINE\SOFTWARE\PHP\x.y\IniFilePath and HKEY_LOCAL_MACHINE\SOFTWARE\PHP\x\IniFilePath, where x, y and z mean the PHP major, minor and release versions.
  • HKEY_LOCAL_MACHINE\SOFTWARE\PHP\IniFilePath (Windows Registry location)
  • Current working directory (except CLI)
  • The web server's directory (for SAPI modules), or directory of PHP (otherwise in Windows)
  • Windows directory (C:\windows or C:\winnt) (for Windows), or --with-config-file-path compile time option

Where a configuration setting may be set

These modes determine when and where a PHP directive may or may not be set, and each directive within the manual refers to one of these modes. For example, some settings may be set within a PHP script using ini_set(), whereas others may require php.ini or httpd.conf.

For example, the output_buffering setting is PHP_INI_PERDIR therefore it may not be set using ini_set(). However, the display_errors directive is PHP_INI_ALL therefore it may be set anywhere, including with ini_set().

How to change configuration settings

Running PHP as an Apache module

When using PHP as an Apache module, you can also change the configuration settings using directives in Apache configuration files (e.g. httpd.conf) and .htaccess files. You will need "AllowOverride Options" or "AllowOverride All" privileges to do so.

There are several Apache directives that allow you to change the PHP configuration from within the Apache configuration files. For a listing of which directives are PHP_INI_ALL, PHP_INI_PERDIR, or PHP_INI_SYSTEM, have a look at the List of php.ini directives appendix.

php_value name value

Sets the value of the specified directive. Can be used only with PHP_INI_ALL and PHP_INI_PERDIR type directives. To clear a previously set value use none as the value.

Note: Don't use php_value to set boolean values. php_flag (see below) should be used instead.

php_flag name on|off

Used to set a boolean configuration directive. Can be used only with PHP_INI_ALL and PHP_INI_PERDIR type directives.

php_admin_value name value

Sets the value of the specified directive. This can not be used in .htaccess files. Any directive type set with php_admin_value can not be overridden by .htaccess or ini_set(). To clear a previously set value use none as the value.

php_admin_flag name on|off

Used to set a boolean configuration directive. This can not be used in .htaccess files. Any directive type set with php_admin_flag can not be overridden by .htaccess.

Changing PHP configuration via the Windows registry

When running PHP on Windows, the configuration values can be modified on a per-directory basis using the Windows registry. The configuration values are stored in the registry key HKLM\SOFTWARE\PHP\Per Directory Values, in the sub-keys corresponding to the path names. For example, configuration values for the directory c:\inetpub\wwwroot would be stored in the key HKLM\SOFTWARE\PHP\Per Directory Values\c\inetpub\wwwroot. The settings for the directory would be active for any script running from this directory or any subdirectory of it. The values under the key should have the name of the PHP configuration directive and the string value. PHP constants in the values are not parsed. However, only configuration values changeable in PHP_INI_USER can be set this way, PHP_INI_PERDIR values can not.

 

PHP Options/Info Functions

phpinfo

Outputs a large amount of information about the current state of PHP. This includes information about PHP compilation options and extensions, the PHP version, server information and environment (if compiled as a module), the PHP environment, OS version information, paths, master and local values of configuration options, HTTP headers, and the PHP License.

Because every system is setup differently, phpinfo() is commonly used to check configuration settings and for available predefined variables on a given system.

phpinfo() is also a valuable debugging tool as it contains all EGPCS (Environment, GET, POST, Cookie, Server) data.

Example #1 phpinfo() Example

<?php

// Show all information, defaults to INFO_ALL
phpinfo();

// Show just the module information.
// phpinfo(8) yields identical results.
phpinfo(INFO_MODULES);

?>

 

get_loaded_extensions

Example #1 get_loaded_extensions() Example

<?php
print_r(get_loaded_extensions());
?>

The above example will output something similar to:

Array

(

[0] => xml

[1] => wddx

[2] => standard

[3] => session

[4] => posix

[5] => pgsql

[6] => pcre

[7] => gd

[8] => ftp

[9] => db

[10] => calendar

[11] => bcmath

)

 

 

get_extension_funcs

This function returns the names of all the functions defined in the module indicated by module_name .

Example #1 Prints the XML functions

<?php
print_r(get_extension_funcs("xml"));
?>

The above example will output something similar to:

Array

(

[0] => xml_parser_create

[1] => xml_parser_create_ns

[2] => xml_set_object

[3] => xml_set_element_handler

[4] => xml_set_character_data_handler

[5] => xml_set_processing_instruction_handler

[6] => xml_set_default_handler

[7] => xml_set_unparsed_entity_decl_handler

[8] => xml_set_notation_decl_handler

[9] => xml_set_external_entity_ref_handler

[10] => xml_set_start_namespace_decl_handler

[11] => xml_set_end_namespace_decl_handler

[12] => xml_parse

[13] => xml_parse_into_struct

[14] => xml_get_error_code

[15] => xml_error_string

[16] => xml_get_current_line_number

[17] => xml_get_current_column_number

[18] => xml_get_current_byte_index

[19] => xml_parser_free

[20] => xml_parser_set_option

[21] => xml_parser_get_option

[22] => utf8_encode

[23] => utf8_decode

)

 

 

extension_loaded

Finds out whether the extension is loaded.

Example #1 extension_loaded() example

<?php
if (!extension_loaded('gd')) {
if (!dl('gd.so')) {
exit;
}
}
?>

 

dl

Loads the PHP extension given by the parameter library .

Use extension_loaded() to test whether a given extension is already available or not. This works on both built-in extensions and dynamically loaded ones (either through php.ini or dl()).

Example #1 dl() examples

<?php
// Example loading an extension based on OS
if (!extension_loaded('sqlite')) {
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
dl('php_sqlite.dll');
} else {
dl('sqlite.so');
}
}

// Or, the PHP_SHLIB_SUFFIX constant is available as of PHP 4.3.0
if (!extension_loaded('sqlite')) {
$prefix = (PHP_SHLIB_SUFFIX === 'dll') ? 'php_' : '';
dl($prefix . 'sqlite.' . PHP_SHLIB_SUFFIX);
}
?>

 

set_include_path

Sets the include_path configuration option for the duration of the script.

Example #1 set_include_path() example

<?php
// Works as of PHP 4.3.0
set_include_path('/inc');

// Works in all PHP versions
ini_set('include_path', '/inc');
?>

Example #2 Adding to the include path

Making use of the PATH_SEPARATOR constant, it is possible to extend the include path regardless of the operating system.

In this example we add /usr/lib/pear to the end of the existing include_path.

<?php
$path = '/usr/lib/pear';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
?>

 

get_include_path

Gets the current include_path configuration option value.

Example #1 get_include_path() example

<?php
// Works as of PHP 4.3.0
echo get_include_path();

// Works in all PHP versions
echo ini_get('include_path');
?>

 

restore_include_path

Restores the include_path configuration option back to its original master value as set in php.ini

Example #1 restore_include_path() example

<?php
echo get_include_path();  // .:/usr/local/lib/php
set_include_path('/inc');
echo get_include_path();  // /inc

// Works as of PHP 4.3.0
restore_include_path();

// Works in all PHP versions
ini_restore('include_path');
echo get_include_path();  // .:/usr/local/lib/php
?>

phpversion

Returns a string containing the version of the currently running PHP parser or extension.

Example #1 phpversion() example

<?php
// prints e.g. 'Current PHP version: 4.1.1'
echo 'Current PHP version: ' . phpversion();

// prints e.g. '2.0' or nothing if the extension isn't enabled
echo phpversion('tidy');
?>

 

ini_set

Sets the value of the given configuration option. The configuration option will keep this new value during the script's execution, and will be restored at the script's ending.

Example #1 ini_restore() example

<?php
$setting = 'y2k_compliance';

echo 'Current value for \'' . $setting . '\': ' . ini_get($setting), PHP_EOL;

ini_set($setting, ini_get($setting) ? 0 : 1);
echo 'New value for \'' . $setting . '\': ' . ini_get($setting), PHP_EOL;

?>

The above example will output:

Current value for 'y2k_compliance': 1

New value for 'y2k_compliance': 0

 

ini_get

Returns the value of the configuration option on success.

Example #1 A few ini_get() examples

<?php
/*
Our php.ini contains the following settings:

display_errors = On
register_globals = Off
post_max_size = 8M
*/

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";
echo 'post_max_size in bytes = ' . return_bytes(ini_get('post_max_size'));

function return_bytes($val) {
$val = trim($val);
$last = strtolower($val[strlen($val)-1]);
switch($last) {
// The 'G' modifier is available since PHP 5.1.0
case 'g':
$val *= 1024;
case 'm':
$val *= 1024;
case 'k':
$val *= 1024;
}

return $val;
}

?>

The above example will output something similar to:

 

display_errors = 1

register_globals = 0

post_max_size = 8M

post_max_size+1 = 9

post_max_size in bytes = 8388608

 

 

 

 

ini_get_all

Returns all the registered configuration options.

Example #1 ini_get_all() examples

<?php
print_r(ini_get_all("pcre"));
print_r(ini_get_all());
?>

The above example will output something similar to:

Array

(

[pcre.backtrack_limit] => Array

(

[global_value] => 100000

[local_value] => 100000

[access] => 7

)

 

[pcre.recursion_limit] => Array

(

[global_value] => 100000

[local_value] => 100000

[access] => 7

)

 

)

Array

(

[allow_call_time_pass_reference] => Array

(

[global_value] => 0

[local_value] => 0

[access] => 6

)

 

[allow_url_fopen] => Array

(

[global_value] => 1

[local_value] => 1

[access] => 4

)

 

...

 

)

 

 

ini_restore

Restores a given configuration option to its original value.

Example #1 ini_restore() example

<?php
$setting = 'y2k_compliance';

echo 'Current value for \'' . $setting . '\': ' . ini_get($setting), PHP_EOL;

ini_set($setting, ini_get($setting) ? 0 : 1);
echo 'New value for \'' . $setting . '\': ' . ini_get($setting), PHP_EOL;

ini_restore($setting);
echo 'Original value for \'' . $setting . '\': ' . ini_get($setting), PHP_EOL;
?>

The above example will output:

Current value for 'y2k_compliance': 1

New value for 'y2k_compliance': 0

Original value for 'y2k_compliance': 1

 

 

set_time_limit

Set the number of seconds a script is allowed to run. If this is reached, the script returns a fatal error. The default limit is 30 seconds or, if it exists, the max_execution_time value defined in the php.ini.

When called, set_time_limit() restarts the timeout counter from zero. In other words, if the timeout is the default 30 seconds, and 25 seconds into script execution a call such as set_time_limit(20) is made, the script will run for a total of 45 seconds before timing out.

<?php
set_time_limit(20);
while ($i<=10)
{
echo "i=$i ";
sleep(100);
$i++;
}
?>

Output:
i=0 i=1 i=2 i=3 i=4 i=5 i=6 i=7 i=8 i=9 i=10