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:
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):
Deny from all
Allow from 123.456.789.000
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.
; 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.
Modifying a Value Via .htaccess
Access to the php.ini is trivial when you are the server administrator, however shared hosting environments will logically prevent you from modifying this file. All is not lost however; if you require certain directives to be set in a specific way in order for your website to run properly on a shared host, and your shared host is running PHP as an Apache module, then it's possible you could modify the desired PHP directives within an .htaccess file which resides in your website's root directory. For instance, if your shared host disables the use of short open tags you can override this setting and enable them by adding this line to your .htaccess file:
php_flag short_open_tag On
The php_flag directive is an Apache-specific configuration feature, so you won't be able to use this when working with other web servers such as Nginx. Further, this is used for setting PHP's Boolean directives; you'll want to use php_value to set those directives which aren't Boolean in nature, such as the include_path.
For reasons of security, not all PHP configuration directives can be changed in this way! Each directive is associated with a mode value which determines exactly where the directive can be modified. Four modes are supported, including:
PHP_INI_USER: The directive can be set within PHP scripts (more about this in the next section).
PHP_INI_PERDIR: The directive can be set in the php.ini file, in httpd.conf, and in .htaccess.
PHP_INI_SYSTEM: The directive can be set in php.ini and httpd.conf.
PHP_INI_ALL: The directive can bet set via any of the aforementioned outlets.
You can view a list of available PHP directives and their corresponding mode values here.
Modifying a Value Via a PHP Script
In the previous set of bullet points I mentioned it was possible to set a PHP directive from within a PHP script. You might wish to take this approach when the need arises to change a directive value which should be localized to one specific script. For instance suppose a particular script occasionally needed to run for longer than expected as it was responsible for generating a lengthy sales report. You can increase the default maximum PHP script execution time by setting the max_execution_time directive at the top of the script using the ini_set() function:
Although not the most exciting of topics, your understanding of PHP's approach to configuration will be a significant contributor to the overall success of future projects. Hopefully this brief introduction provides you with a solid foundation for performing further investigation!