PHP - Configuration

php

How can we comment out a directive in the php.ini file?

Put the semicolon at the beginning of the line.

How can we use environment variables inside the php.ini file?

; PHP_MEMORY_LIMIT is taken from environment
memory_limit = ${PHP_MEMORY_LIMIT}

How can we reference a php.ini directive from inside the php.ini file itself?

Since PHP 5.1.0, it is possible to refer to existing .ini variables from within .ini files. Example:

open_basedir = ${open_basedir} ":/new/dir"

How can we cause PHP to look for the php.ini in a different directory?

It is possible to configure PHP to scan for .ini files in a directory after reading php.ini. This can be done at compile time by setting the —with-config-file-scan-dir option. In PHP 5.2.0 and later, the scan directory can then be overridden at run time by setting the PHP_INI_SCAN_DIR environment variable.

It is possible to scan multiple directories by separating them with the platform-specific path separator (; on Windows, NetWare and RISC OS; : on all other platforms; the value PHP is using is available as the PATH_SEPARATOR constant). If a blank directory is given in PHP_INI_SCAN_DIR, PHP will also scan the directory given at compile time via --with-config-file-scan-dir .

How does PHP scan the specified directory for configuration files?

Within each directory, PHP will scan all files ending in .ini in alphabetical order. A list of the files that were loaded, and in what order, is available by calling php_ini_scanned_files(), or by running PHP with the --ini option.

What is the purpose of the php_ini_scanned_files() function?

This function returns an array of configuration files that was processed.

What is the purpose of the user.ini file?

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. Two new INI directives, user_ini.filename and user_ini.cache_ttl control the use of user INI files. user_ini.filename sets the name of the file PHP looks for in each directory; if set to an empty string, PHP doesn't scan at all. The default is .user.ini. The user_ini.cache_ttl setting controls how often user INI files are re-read. The default is 300 seconds (5 minutes).

What are PHP_INI_* modes?

  1. PHP_INI_ALL: Entry can be set anywhere
  2. PHP_INI_SYSTEM: Entry can be set in php.ini or httpd.conf
  3. PHP_INI_PERDIR: Entry can be set in php.ini, .htaccess, httpd.conf or .user.ini (since PHP 5.3)
  4. 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

How can we use php_value, php_flag, php_admin_value, and php_admin_flag?

There are several Apache directives that allow you to change the PHP configuration from within the Apache configuration files.

  1. 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. Don't use php_value to set boolean values. php_flag should be used instead.
  2. 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.
  3. 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.
  4. 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().
<IfModule mod_php5.c>
  php_value include_path ".:/usr/local/lib/php"
  php_admin_flag engine on
</IfModule>
<IfModule mod_php4.c>
  php_value include_path ".:/usr/local/lib/php"
  php_admin_flag engine on
</IfModule>

PHP constants do not exist outside of PHP. For example, in httpd.conf you can not use PHP constants such as E_ALL or E_NOTICE to set the error_reporting directive as they will have no meaning and will evaluate to 0. Use the associated bitmask values instead. These constants can be used in php.ini

Can we change some configuration settings at runtime?

Yes, but only for some settings. Regardless of how you run PHP, you can change certain values at runtime of your scripts through ini_set(). See the documentation on the ini_set() page for more information.

How can we access some configuration settings at runtime?

We can access the values of individual configuration directives at runtime using ini_get() or get_cfg_var().

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License