PHP Runtime Configuration

By:    Updated: January 24,2017

The configuration file

The main 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 versions, it happens on every invocation.

 

If you can change the php.ini, you should restart your web server when you use php as php-module with apache, or reload php-fpm when you use php under FastCGI with apache or nginx.

<?php
//You can use php_sapi_name() to get php running mode.
echo php_sapi_name(); //It will be one of fpm-fcgi, cli, apache2handler and so forth
?>

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.

Definition of PHP_INI_* modes
Mode Meaning
PHP_INI_USER Entry can be set in user scripts (like with ini_set()) or in the Windows registry.
Since PHP 5.3, entry can be set in .user.ini
PHP_INI_PERDIR Entry can be set in php.ini, .htaccess, httpd.conf or .user.ini (since PHP 5.3)
PHP_INI_SYSTEM Entry can be set in php.ini or httpd.conf
PHP_INI_ALL Entry can be set anywhere

 

You can check the php.ini (local file under installation directory or online manual) which consist of most directives.

.user.ini files

Since PHP 5.3.0, PHP includes support for configuration INI files on a per-directory basis. These files are processed only by the CGI/FastCGI SAPI. This functionality obsoletes the PECL htscanner extension. If you are using Apache, use .htaccess files for the same effect.

 

In addition to the main php.ini file, PHP scans for INI files in each directory, starting with the directory of the requested PHP file, and working its way up to the current document root (as set in $_SERVER['DOCUMENT_ROOT']). In case the PHP file is outside the document root, only its directory is scanned.

 

Only INI settings with the modes PHP_INI_PERDIR and PHP_INI_USER will be recognized in .user.ini-style INI files.

//in php.ini
user_ini.filename  = ".user.ini"
user_ini.cache_ttl = 300

Change settings in httpd.conf or .htaccess

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.

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.

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 or ini_set().

//in httpd.conf or .htaccess
php_value max_execution_time 60
php_flag  file_uploads       on

//only in httpd.conf
php_admin_value memory_limit 256M
php_admin_flag  log_errors   on

ini_set()

If you are interested in a complete list of configuration settings on your system with their current values, you can execute the phpinfo() function, and review the resulting page. You can also access the values of individual configuration directives at runtime using ini_get() or get_cfg_var().

<?php
ini_set('display_errors', 1);
?>

 

More in Development Center
New on Valinv
Sources
  • http://php.net/manual/en/configuration.php
Related Articles
Sponsored Links