Configuring PHP

PHP uses settings in a file named php.ini to control some of its behavior. PHP looks for php.ini when it begins and uses the settings that it finds. If PHP can’t find the file, it uses a set of default settings. The default location for the php.ini file is one of the following unless you change it during installation:

If the php.ini file isn’t installed during installation, you need to install it now. A configuration file with default settings, called php.ini-dist, is included in the PHP distribution. Copy this file into the appropriate location, such as the default locations just mentioned, changing its name to php.ini.

If you have a previous version of PHP installed (such as PHP 4.3), make a backup copy of the php.ini file before you overwrite it with the php.ini file for PHP 5/6. You can then see the settings you are currently using and change the settings in the new php.ini file to match the current settings.

To configure PHP, follow these steps:

  1. Open the php.ini file for editing.
  2. Change the settings you want to change.

    Steps 3, 4, 5, and 6 mention some specific settings that should always be changed if you are using the specified environment.

  3. Only if you are using PHP 5 or earlier, turn off magic quotes.

    Look for the following line:

       magic_quotes-gpc On
    Change On to Off.

  4. Only if you are using PHP 5/6 on Windows, activate mysqli or mysql support. See instructions in the section, Activating MySQL Support.
  5. Only if you’re using PHP on Windows with the IIS Web server, turn off force redirect. Find the line:
       ;cgi.force_redirect = 1
    You need to remove the semicolon so that the setting is active, and also change the 1 to 0. After the changes, the line looks as follows:
       cgi.force_redirect = 0 
  6. Only if you're using PHP 5 or later, set your local timezone. Find the line:
       ;date.timezone =
    Remove the semicolon from the beginning of the line. Add the code for your local timezone after the equal sign. For instance, the line might be:
       date.timezone = America/Los_Angeles
    You can find a list of timezone codes at www.php.net/manual/en/timezones.php.
  7. Save the php.ini file.
  8. Restart your Web server so that the new settings go into effect.
In general, the remaining default settings allow PHP to run okay, but you might need to edit some of these settings for specific reasons. I discuss settings in the php.ini file throughout the book when I discuss a topic that might require you to change settings.