One of the most powerful features of PHP is the ability to customize its behavior through a configuration file. In this first article, we're going to cover some of the ways the
php.ini file can be used and how it might help you. The second article will outline in detail many of the more frequently used directives of the
Using the PHP configuration file can make administration of PHP as simple as administration of Apache! Depending on your web server preferences, that could be good or bad. But we'll just assume it's good for the sake of this article. The php.ini file is used by PHP to simplify post-installation configuration of PHP and gives you one place for all of PHP's runtime configurations. Wouldn't it just be a pain to have to recompile PHP for every little change in behavior?
When PHP fires up its engines, one of the first thing it does is to look for the
php.ini file so that it can read into memory the directives defined therein. It looks for php.ini in the current working directory, the path designated by the environmental variable
PHPRC, and in the path defined at compile time. Most of the time when you compile PHP, it places a copy of the ini file in
If you are on a Unix machine, you may be able to type
locate php.ini or
find / -name php.ini -print and have it tell you the location of the
php.ini file. If you have installed it on a Windows machine, use the "Find -> Files or Folders" option from the Start Menu.
As I said before, the
php.ini file provides the advantage of allowing you to dynamically customize PHP. Well, dynamically is only half true. If you're running PHP as an Apache module, the configuration directives reside in memory until a new Apache process has been started. Since a CGI program starts a new process each time, the configuration information is re-read each time PHP is used as a CGI program, thus giving the impression of dynamic configuration. Otherwise, the PHP configuration remains unchanged until the web server is restarted. If you are using Apache,
apachectl restart will do the trick.
In previous versions of PHP you could include the configuration directives as Apache directives in either the
.htaccess files. With version 4.x, all directives must reside in the
php.ini file with the exception of
php_admin_flag. The following is a brief description of what these four directives do as well as their usage:
php_value name value
This directive allows you to essentially define your own directives. Just enter the directive php_value followed by the name of your custom directive and then the value that should be associated with that directive.
php_flag name on|off
This directive is similar to php_value except that it allows you to define a custom directive with a Boolean ("on" or "off") value.
php_admin_value name value
Also similar to php_value, this directive can only be defined in the Apache httpd.conf file.
php_admin_flag name on|off
The Boolean version of php_admin_value.
Because of the power of
php.ini, some security issues must be noted or the world will end. OK, well, it might not actually "end." But it sure might feel as if it did. If you're using PHP as a CGI, then you should always set the
safe_mode directive to "On." Then set the
safe_mode_exec_dir directive. By doing this, you're ensuring that the user can only access information contained in his document root.
Also note that if you enable dynamic module loading with the
enable_dl directive, it's possible the
safe_mode restrictions could be bypassed.
The structure of the
php.ini file is a standard "directive = value" syntax, very much in the style of Windows .ini files. Lines consisting of only white space and lines beginning with a semicolon are ignored. The semicolon is used to add comments to the file. To give a directive an empty value, you can either leave the value blank or use the word
none. Sections are defined with "
[" and "
]" with the section name sandwiched in between (i.e., "
[custom stuff]"). You probably don't need to worry about sections very much except to make reading the configuration file a little easier.
Now that you have located your
php.ini file, it can be edited with any text editor. In Unix,
vi is the standard, with
emacs popular alternatives. Windows users can use Notepad, but be careful when using programs like Microsoft Word that change the format of the document. System administration is best done with a text editor instead of a word processor.
In the next article I cover in detail some of the more powerful directives of the PHP configuration file, so stay tuned!
Darrell Brogdon is a web developer for SourceForge at VA Linux Systems and has been using PHP since 1996.
Read more PHP Admin Basics columns.
Discuss this article in the O'Reilly Network PHP Forum.
Return to the PHP DevCenter.
Copyright © 2009 O'Reilly Media, Inc.