Working with the php.ini File

October 29, 2008

Being able to work with the php.ini file goes along way towards mastering PHP. If you are developing scripts on your own PHP-enabled computer or if you have administrative level control over your server, you can globally change how your PHP installation runs.

Common settings you might want to adjust include:

  • register_globals
  • display_errors
  • error_reporting
  • magic_quotes_gpc

Keep in mind that PHP is installed with the settings that the developers think are best. Changing any particular setting may make it easier to learn the language but at the cost of security or performance. That being said, I present to you…

Steps for editing your php.ini file:

  1. Create a script called phpinfo.php containing the following code:
    <?php
    phpinfo();
    ?>
    
  2. Place this script on your server.
  3. Run this script in your Web browser by going to http://your.url.here/phpinfo.php in your Web browser (on your own computer, this may be something like http://localhost/phpinfo.php or http://localhost/~username/phpinfo.php).
  4. In the resulting page, look for the line which says “Configuration File (php.ini) Path”. It should be about 6 rows down in the table.
  5. Note the location of your php.ini file. This is the active file PHP is using. Your server may have multiple php.ini files on it but this is the one that counts.
  6. If you don’t have a php.ini file on your server (if the value is blank in the phpinfo.php script or if you go to the directory it lists and there’s no php.ini file there), create one by:
    1. Downloading the complete source code from http://www.php.net/downloads.php.
    2. Extract the source (unzip or whatever)j so that you now have a folder of files.
    3. Find the php.ini-dist file, located within the main folder.
    4. Rename this file as php.ini and move it to the proper directory.
  7. Open the php.ini file in a text editor (don’t use Notepad, Word, TextEdit, or any other text editor which will not properly display line or will not let you save a plain text file).
  8. Make any changes you want, keeping in mind the following:
    • Comments are marked using a semi-colon. Anything after the semi-colon is ignored.
    • Instructions on what most of the settings mean are included in the file.
    • The top of the page lists general information with examples. Do not change these values. Change the settings where they appear later in the file.
    • For safety purposes, don’t change any original settings, just comment them out (by preceding the line with a semi-colon), then add the new, modified line afterwards.
    • Add a comment (using the semi-colon) to mark what changes you made and when. For example:
      ; register_globals = Off
      register_globals = On ; Added by LEU 4/8/2004
      
  9. Save the file.
  10. Restart the Web server (Apache, IIS, Xitami, etc.). You do not need to restart your whole computer.
  11. Rerun the phpinfo.php script to make sure the changes took effect.