PHP Configuration Directivesby Darrell Brogdon
In a previous article, I gave an overview of what the
php.ini file is and how you might use it. Now let's get into detail on some of the more useful directives and learn how to use them.
One of the newest (and greatest) features of PHP is the ability to send header lines anywhere within a script. This directive turns on output buffering for all files. You should probably leave this set to "off" because calling one of the output buffering functions of PHP will work regardless.
Turn on output buffering.
output_buffering = On
This directive is used mainly for debugging purposes because of performance. Used with
output_bufferingit is equivalent to calling the
flush()function after every
echo(), and HTML block.
Turn off implicit flushing of output buffers.
implicit_flush = Off
When PHP is used as a CGI this will ensure that the user can only access information contained in the document root.
Turn off safe mode.
safe_mode = Off
This allows you to set a prefix to any environmental variables ensuring that only variables you accept can be accessed/set. Multiple prefixes can be set as a comma-delimited list.
Only allow PHP and HTTP environmental variables.
safe_mode_allowed_env_vars = PHP_, HTTP_
Allows you to define, by a comma-delimited list, any environmental variables that can never be changed.
Protect all HTTP environmental variables.
safe_mode_protected_env_vars = HTTP_
Allows you to define a comma-delimited list of functions to be disabled within PHP.
Disable certain file-related functions.
disable_functions = fopen, fwrite, popen
Maximum time a PHP script will execute before timing out. Even if this is set to "off", the execution time can be defined on a per-script basis using the
Set maximum execution time to 1 minute.
max_execution_time = 60
Maximum amount of memory a PHP script can consume.
Limit memory to approximately 8 Mbits.
memory_limit = 8388608
This is a bit-field or named constant directive with the following bit levels added to determine the verbosity of PHP errors.
1 - E_ERROR Normal errors 2 - E_WARNING Normal warnings 4 - E_PARSE Parser errors 8 - E_NOTICE Non-critical style-related errorsExample:
Only report the three most critical errors.
error_reporting = E_ERROR | E_WARNING | E_PARSE
Show all errors except notices (shorthand for above example)
error_reporting = E_ALL & ~E_NOTICE
This will cause PHP to print out the errors along with the HTML. Helpful for debugging.
Display errors to the screen.
display_errors = On
Also in PHP Admin Basics:
This directive causes PHP to store errors in a log file which can be defined with the
Log all errors to a log file.
log_errors = On
Stores the last error/warning message in a PHP variable called
Enable the tracking of last error/warning message.
track_errors = On
String to output before an error message. This is handy if you want to make all of your errors highlighted in red, for example.
Highlight all errors in red.
error_prepend_string = "<span style='color: red;'>"
The opposite of
error_append_string = "</span>"
The directive defines which file you would like PHP to use to log errors. If you want errors to be logged with
syslog, you can give the value "syslog" (less the quotes). If you're using NT, this will log errors in the Event Log.
Log all errors to '/var/log/my_php_errors.log'
error_log = /var/log/my_php_errors.log
This turns on
Enable track variables.
track_vars = On
Turns on magic quotes (escapes single quotes, double quotes, null, and backslash characters) for
POST, and cookie data.
Enable magic quotes.
magic_quotes_gpc = On
Automatically escape data generated externally.
Disable magic quotes runtime.
magic_quotes_runtime = Off
In addition to escaping with the standard backslash character (
\) this tells PHP to use a single quote as an escape character for Sybase-related escapes.
Disable magic quotes for Sybase.
magic_quotes_sybase = Off
This tells PHP to automatically tack on the file defined with this directive to the beginning of any file parsed by the PHP engine. This is very useful for defining global variables and making database connections.
Automatically prepend the file
auto_prepend_file = pre.php
This tells PHP to automatically tack on the file defined with this directive to the end of any file parsed by the PHP engine.
Automatically append the file
auto_append_file = post.php
Customize the character set of PHP parsed pages.
Set the default charicter set to Latin-1.
default_charset = "iso-8859-1"
This tells PHP where to look when the
require()functions are used. The format for Unix is just like that of the
PATHenvironmental variable. This is pretty much the same on Windows except that the colon is replaced with a semicolon.
Set the include path to be the current directiory and
include_path = .:/usr/local/lib/php
Windows version of the above example.
include_path = .;/usr/local/lib/php
This defines where PHP will store uploaded files temporarily until the user can move them to a more permanent location.
Store all uploaded files in
upload_tmp_dir = /tmp
The maximum file size that can be uploaded through PHP.
Set the maximum uploaded file size to be 2 Mbits.
upload_max_filesize = 2097152
This defines the location where loadable module extensions reside.
Set the current working directory as the extension directory.
extension_dir = ./
This enables/disables the ability to dynamically load a PHP module extension. This doesn't work properly on multithreaded servers so it is disabled by default on those systems.
Enable dynamically loadable modules.
enable_dl = On
This directive defines a dynamically loadable module extension that PHP should load when starting up. This directive can be used any number of times for each extension that you need to load.
Enable dynamic loading of the mSQL module.
extension = msql.so
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.