Configuration Settings


PHP allows you to tune many aspects of its behavior by using a set of configuration directives. These directives can be global for your entire web server, or you can make local changes that apply only to certain scripts.

Using php.ini

PHP's configuration file is named php.ini. Its location is set at compile time; by default, it is located in /usr/local/lib/php.ini on Linux/Unix servers and C:\WINDOWS\php.ini on Windows systems.

The php.ini file contains a list of configuration directives and their values, separated by equals signs. The default php.ini file distributed with PHP is well documented, with plenty of comments. Any line that begins with a semicolon is considered a comment, and sections of the file are broken up using headings in square brackets, which the compiler also ignores.

Listing 23.1 shows an extract from an unchanged php.ini file for PHP 5 that contains the log settings. As you can see, for many setting changes, you do not even need to refer to the online documentation.

Listing 23.1. An Extract from php.ini
 ; Print out errors (as a part of the output).  For  ; production web sites, ; you're strongly encouraged to turn this feature off, ; and use error logging ; instead (see below).  Keeping display_errors enabled ; on a production web site ; may reveal security information to end users, such as ; file paths on your Web ; server, your database schema or other information. display_errors = On ; Even when display_errors is on, errors that occur  ; during PHP's startup ; sequence are not displayed.  It's strongly recommended ; to keep ; display_startup_errors off, except for when debugging. display_startup_errors = Off ; Log errors into a log file (server-specific log, stderr, ; or error_log (below)) ; As stated above, you're strongly advised to use error  ; logging in place of ; error displaying on production web sites. log_errors = Off ; Set maximum length of log_errors. In error_log information ; about the source is ; added. The default is 1024 and 0 allows to not apply any  ;maximum length at all . log_errors_max_len = 1024 

True or False Boolean values in php.ini can be set to true (that is, on or yes) or false (that is, off, no, or none). These values are not case-sensitive.


When it runs as a web server module, php.ini is read when the web server process starts, and changes made to the configuration file do not take place until the web server is restarted.

If your web server runs PHP as a CGI binary, the php.ini settings are loaded each time a script is run because a new php process is started. Similarly, command-line PHP loads the settings from php.ini each time a script is run.

Alternate php.ini Files

You can create separate php.ini files to apply for the different ways PHP can be run. If you create a file named php-SAPI.ini (replacing SAPI with the a valid SAPI name), that file is read instead of the global php.ini.

For instance, to provide a different set of directives only for command-line PHP, you would use a configuration file named php-cli.ini. For the Apache web server module, the filename would be php-apache.ini.

On a Windows system, a php.ini file in the Apache installation directory is used before one in C:\WINDOWS. This allows you to maintain different PHP settings for multiple web servers on the same machine.

To force the use of a particular configuration file, you must invoke php with the c option. In a shell script, you might change the first line to the following to force a custom configuration file to be used only for that script:

 #!/usr/local/bin/php c /path/to/php.ini 

Per-Directory Configuration

Apache web server allows you to use a per-directory configuration file named .htaccess to supply custom web server directives. PHP supports the use of .htaccess to override the global settings from php.ini.

To give a new value for a PHP setting, you use php_value followed by the directive from php.ini and the new value. The following line in an .htaccess file gives a new value for max_execution_time of 60 seconds:

 php_value max_execution_time 60 

Using .htaccess Be aware of the syntax difference when changing configuration settings in php.ini and .htaccess. In php.ini there must be an equals sign between the directive name and the value. In .htaccess the value follows the directive name, with no equals sign.


Changes made in .htaccess apply only to the directory in which it resides and its subdirectories. Any settings in .htaccess override the global php.ini as well as any settings made in an .htaccess file in a parent directory.

Dynamic Configuration

You can alter values of directives set in php.ini on-the-fly by using the ini_set function. It takes two arguments: the directive name and the new value. When you change a setting by using ini_set, the return value is the previous setting for that directive.

The following example changes the memory_limit setting for the current script to run a section of code that may require more resources than usual:

 $limit = ini_set("memory_limit", "128M"); // Execute code that requires this setting ini_set("memory_limit", $limit); 

The previous value is saved to a variable and then restored when the intensive code has completed.

To find the current value of any php.ini setting without changing it, you use the ini_get function.



    Sams Teach Yourself PHP in 10 Minutes
    Sams Teach Yourself PHP in 10 Minutes
    ISBN: 0672327627
    EAN: 2147483647
    Year: 2005
    Pages: 151
    Authors: Chris Newman

    Similar book on Amazon

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