PHP | php.ini File Configuration
At the time of PHP installation, php.ini is a special file provided as a default configuration file. It’s very essential configuration file which controls, what a user can or cannot do with the website. Each time PHP is initialized, the php.ini file is read by the system. Sometimes you need to change the behavior of PHP at runtime, then this configuration file is to use.
All the settings related to register global variables, upload maximum size, display log errors, resource limits, the maximum time to execute a PHP script and others are written in a file as a set of directives which helps in declaring changes.
Note: Whenever some changes are performed in the file, you need to restart our web server.
It helps in easy administration of web server using these configuration files. We can also write our own custom configuration files.
To check file path use the following program:
php
Note: Keys in the file are case-sensitive, keyword values are not spaces and lines starting with a semicolon are ignored. The file is well commented. The Boolean values are represented by On/Off, 1/0, True/False, Yes/No.
The file contains a set of directives with a set of respective values assigned to it. The values can be string, a number, a PHP constant, INI constants, or an expression, a quoted string or a reference to a previously set variable. Expression in the INI file is limited to bitwise operators or parentheses. Settings with a particular hostname will work under that particular host only.
Environment variables of php.ini file:
- memory_limit: This setting is done to show the maximum amount of memory a script consumes.
Important settings or common parameters of the php.ini file:
- enable_safe_mode = on Its default setting to ON whenever PHP is compiled. Safe mode is most relevant to CGI use.
- register_globals = on its default setting to ON which tells that the contents of EGPCS (Environment, GET, POST, Cookie, Server) variables are registered as global variables. But due to a security risk, the user has to ensure if it set to OFF for all scripts.
- upload_max_filesize This setting is for the maximum allowed size for uploaded files in the scripts.
- upload_tmp_dir = [DIR] Don’t uncomment this setting.
- post_max_size This setting is for the maximum allowed size of POST data that PHP will accept.
- display_errors = off This setting will not allow showing errors while running PHP project in the specified host.
- error_reporting = E_ALL & ~E_NOTICE: This setting has default values as E_ALL and ~E_NOTICE which shows all errors except notices.
- error_prepend_string = [“”] This setting allow you to make different color of messages.
- max_execution_time = 30 Maximum execution time is set to seconds for any script to limit the time in production servers.
- short_open_tags = Off To use XML functions, we have to set this option as off.
- session.save-handler = files You don’t need to change anything in this setting.
- variables_order = EGPCS This setting is done to set the order of variables as Environment, GET, POST, COOKIE, SERVER. The developer can change the order as per the need also.
- warn_plus_overloading = Off This setting issues a warning if + used with strings in a form of value.
- gpc_order = GPC This setting has been GPC Deprecated.
- magic_quotes_gpc = on This setting is done in case of many forms are used which submits to themselves or others and display form values.
- magic_quotes_runtime = Off If magic_quotes_sybase is set to On, this must be Off, this setting escape quotes.
- magic_quotes_sybase = Off If this setting is set to off it should be off, this setting escape quotes.
- auto-prepend-file = [filepath] This setting is done when we need to automatically include() it at the beginning of every PHP file.
- auto-append-file = [filepath] This setting is done when we need to automatically include() it at the end of every PHP file.
- include_path = [DIR] This setting is done when we need to require files from the specified directories. Multiple directories are set using colons.
- ignore_user_abort = [On/Off] This settings control what will happen when the user click any stop button. The default value is on this setting doesn’t work on CGI mode it works on only module mode.
- doc_root = [DIR] This setting is done if we want to apply PHP to a portion of our website.
- file_uploads = [on/off] This flag is set to ON if file uploads are included in the PHP code.
- mysql.default_host = hostname This setting is done to connect to MySQL default server if no other server host is mentioned.
- mysql.default_user = username This setting is done to connect MySQL default username, if no other name is mentioned.
- mysql.default_password = password This setting is done to connect MySQL default password if no other password is mentioned.
Configuration of php.ini file: Whenever we install PHP, we can locate the configuration file inside the PHP folder. If using xampp, we can find the configuration file in one or many versions, inside the path ‘\xampp\php’.
Note: Other versions of this file are php.ini-development and php.ini-production. The most preferred one is php.ini-development file.
Php default config 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 versions, it happens on every invocation.
- SAPI module specific location ( PHPIniDir directive in Apache 2, -c command line option in CGI and CLI)
- The PHPRC environment variable.
- The location of the php.ini file can be set for different versions of PHP. The root of the registry keys depends on 32- or 64-bitness of the installed OS and PHP. For 32-bit PHP on a 32-bit OS or a 64-bit PHP on a 64-bit OS use [(HKEY_LOCAL_MACHINE\SOFTWARE\PHP] for 32-bit version of PHP on a 64-bit OS use [HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\PHP] ] instead. For same bitness installation the following registry keys are examined in order: [HKEY_LOCAL_MACHINE\SOFTWARE\PHP\x.y.z] , [HKEY_LOCAL_MACHINE\SOFTWARE\PHP\x.y] and [HKEY_LOCAL_MACHINE\SOFTWARE\PHP\x] , where x, y and z mean the PHP major, minor and release versions. For 32 bit versions of PHP on a 64 bit OS the following registry keys are examined in order: [HKEY_LOCAL_MACHINE\SOFTWARE\WOW6421Node\PHP\x.y.z] , [HKEY_LOCAL_MACHINE\SOFTWARE\WOW6421Node\PHP\x.y] and [HKEY_LOCAL_MACHINE\SOFTWARE\WOW6421Node\PHP\x] , where x, y and z mean the PHP major, minor and release versions. If there is a value for IniFilePath in any of these keys, the first one found will be used as the location of the php.ini (Windows only).
- [HKEY_LOCAL_MACHINE\SOFTWARE\PHP] or [HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\PHP] , value of IniFilePath (Windows only).
- 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.
If php-SAPI.ini exists (where SAPI is the SAPI in use, so, for example, php-cli.ini or php-apache.ini ), it is used instead of php.ini . The SAPI name can be determined with php_sapi_name() .
Note:
The Apache web server changes the directory to root at startup, causing PHP to attempt to read php.ini from the root filesystem if it exists.
Using environment variables can be used in php.ini as shown below.
Example #1 php.ini Environment Variables
; PHP_MEMORY_LIMIT is taken from environment memory_limit = $
The php.ini directives handled by extensions are documented on the respective pages of the extensions themselves. A list of the core directives is available in the appendix. Not all PHP directives are necessarily documented in this manual: for a complete list of directives available in your PHP version, please read your well commented php.ini file. Alternatively, you may find » the latest php.ini from Git helpful too.
Example #2 php.ini example
; any text on a line after an unquoted semicolon (;) is ignored [php] ; section markers (text within square brackets) are also ignored ; Boolean values can be set to either: ; true, on, yes ; or false, off, no, none register_globals = off track_errors = yes ; you can enclose strings in double-quotes include_path = ".:/usr/local/lib/php" ; backslashes are treated the same as any other character include_path = ".;c:\php\lib"
It is possible to refer to existing .ini variables from within .ini files. Example: open_basedir = $ «:/new/dir» .
Scan directories
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. 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.
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.
Assuming PHP is configured with --with-config-file-scan-dir=/etc/php.d, and that the path separator is . $ php PHP will load all files in /etc/php.d/*.ini as configuration files. $ PHP_INI_SCAN_DIR=/usr/local/etc/php.d php PHP will load all files in /usr/local/etc/php.d/*.ini as configuration files. $ PHP_INI_SCAN_DIR=:/usr/local/etc/php.d php PHP will load all files in /etc/php.d/*.ini, then /usr/local/etc/php.d/*.ini as configuration files. $ PHP_INI_SCAN_DIR=/usr/local/etc/php.d: php PHP will load all files in /usr/local/etc/php.d/*.ini, then /etc/php.d/*.ini as configuration files.
Production-friendly Configuration Files in PHP
In config.php , first we include the correct config file according to the server name. Make sure you replace the domains with your production and development domains in the following code. Then, we can include the default configurations file.
if ($_SERVER['SERVER_NAME'] === 'production.com') include_once 'config.server.php'; > else if ($_SERVER['SERVER_NAME'] === 'localdomain.com') include_once 'config.dev.php'; > include_once 'config.default.php';
Each config file can contain code as following.
// database config define('DB_HOST', 'localhost'); define('DB_USERNAME', 'root'); // . // domain config define('PROTOCOL', 'https://'); // . // and any thing
The default config file can contain configurations which are common to both production and development.
define('MAX_USERNAME_LENGTH', 15); // etc.
Now, in any other PHP file where you need configurations to be included, you can just include the config.php file.
include_once '/path/to/config.php';
You may already have used this technique previously. But, if you haven’t I’m happy you learned something new. I think this is the best way to save configs as there’s no need to rename files in production server each time you upload, which makes no sense. (I have seen some people do that)