Back to Basics: Managing PHP Configuration php.ini Directives

PHP is bundled with well over 100 configuration variables (known as directives) which developers can use to configure every conceivable aspect of PHP's behavior. The impact of these variables ranges widely, with some capable of tweaking very fundamental behaviors such as whether the PHP engine will recognize the use of "short tags", and others focusing on specific features such as determining whether sessions are automatically enabled.

While PHP's configuration capabilities are indeed powerful, the sheer breadth and different ways in which these configuration directives can be set are often confusing and downright intimidating to newcomers. So in this article it worth meandering from the typically intermediate-level discussion and instead offer some insight into PHP's configuration-specific infrastructure.

Viewing Your Configuration

The easiest way to learn more about your server's PHP configuration is by executing the phpinfo() function within a PHP script and viewing its output within the browser. The script is an easy one, consisting of just three lines:

<?php
phpinfo();
?>

Save the file using any name you please (phpinfo.php for instance), making sure it uses the typical .php extension. Call it within the browser and you'll be presented with a rather lengthy series of tabular output which begins in a fashion similar to that found in Figure 1. If this is new territory for you, I suggest spending some time perusing this output in order to familiarize yourself with the various ways in which PHP can be configured.

Incidentally, you should take care to not leave this script on a public server, as it contains quite a bit of information which could be useful to any unsavory individual interested in compromising your server. The easiest way to prevent access is simply to remove it altogether. Otherwise if you plan on regularly reviewing your configuration settings using this script, consider restricting access to your laptop's IP address by creating an .htaccess file and adding the following contents (in the following example, you'll need to replace the placeholder 123.456.789.000 IP address with your own):

<Files phpinfo.php>
Order Deny,Allow
Deny from all
Allow from 123.456.789.000
</Files>

Should you desire to modify any of the settings found in the phpinfo() function's output, several approaches are possible, beginning with the php.ini file.

Introducing the php.ini File

The php.ini file is PHP's primary configuration file, containing the vast majority of its configuration directives. Exactly when this file is read by PHP depends upon how PHP is installed. If installed as a server module (the most common approach), it is read only when the web server is started. If PHP's CGI version is installed (or you are using PHP's CLI version), then it will be read each and every time the PHP engine is invoked. Although this file can reside in a great many locations, you'll almost certainly find it within PHP's installation directory. You can easily determine its location by loading the aforementioned phpinfo() script and reviewing the path associated with the Loaded Configuration File setting.

Once located, open this file in a text editor. The beginning of a typical php.ini file is presented below. Each configuration directive is assigned using a directive_name = value syntax. For instance, the first directive, engine, is set to On, meaning the PHP engine is enabled. Semicolons are used to signify the start of a comment. You'll see that the developers have made copious use of comments in order to help new developers sort out the purpose of each directive.

[PHP]
;;;;;;;;;;;;;;;;;;;;
; Language Options ;
;;;;;;;;;;;;;;;;;;;;
; Enable the PHP scripting language engine under Apache.
engine = On
...
; Allow the <? tag. Otherwise, only <?php and <script> tags are recognized.
; NOTE: Using short tags should be avoided when developing applications or
; libraries that are meant for redistribution, or deployment on PHP
; servers which are not under your control, because short tags may not
; be supported on the target server. For portable, redistributable code,
; be sure not to use short tags.
short_open_tag = On

To change a directive, merely modify its value and save the file. If you're running PHP as a server module you'll need to restart the web server in order for the changes to take effect. Otherwise if you're running the CGI or CLI versions these changes will take effect automatically.