Configuring PHP is easy. You can change almost any aspect of the interpreter within the php.ini configuration file, e.g. modify error handling, increase memory usage, etc.
Unfortunately, problems can occur when you move your application to a live hosting environment or are distributing the code to customers. ISPs usually lock down the php.ini configuration file — especially on shared hosting. This could cause your application to fail.
Fortunately, it’s not necessary to upgrade to an expensive dedicated server. The two methods below allow you to override PHP settings within your application.
Apache Module Directives
The majority of ISPs provide Apache web server hosting on Linux or Unix platforms. Hopefully, they’ve also granted “AllowOverride Options” or “AllowOverride All” privileges in Apache’s httpd.conf configuration. This allows you to create an .htaccess file within your application’s root folder overrides the default Apache and PHP configuration.
Two PHP directives are permitted within .htaccess:
- php_flag <boolean-flag-name> on|off
- php_value <flag-name> <flag-value>
php_flag should be used for on/off values, whereas php_value can be used for any others. For example, the following .htaccess file will disable globals, set the maximum file upload size to 20MB, and allow PHP scripts to run for 10 minutes (600 seconds):
php_flag register_globals off php_value upload_max_filesize 20M php_value max_execution_time 600
However, the solution will not work in all Apache installations or other web servers such as IIS.
PHP Runtime Configuration
A more portable, server-agnostic solution is PHP’s ini_set function. — it allows you to change a setting within your application at runtime. The function accepts two arguments: ini_set(flag-name, flag-value), e.g.
<?php ini_set('register_globals', 0); ini_set('upload_max_filesize', '20M'); ini_set('max_execution_time', 600); ?>
Booleans, numbers and strings can be used interchangeably — PHP will attempt to cast the value to an appropriate type.
Several related functions are available:
Returns the configuration value. I’d recommend checking your configuration change and taking appropriate action. Don’t assume ini_get() will always work.
Returns all configuration values as an associative array. The optional extension parameter returns options specific to that extension, e.g. ‘allow_url_fopen’.
Returns the original configuration value from php.ini (not any overrides set in .htaccess or by ini_set).
Returns a configuration option to its original value.
Coming soon: How to Handle Unloaded PHP Extensions.
Has PHP configuration ever caused you problems when porting an application to another server?
Craig is a freelance UK web consultant who built his first page for IE2.0 in 1995. Since that time he's been advocating standards, accessibility, and best-practice HTML5 techniques. He's created enterprise specifications, websites and online applications for companies and organisations including the UK Parliament, the European Parliament, the Department of Energy & Climate Change, Microsoft, and more. He's written more than 1,000 articles for SitePoint and you can find him @craigbuckler.
Your First Year in Code
Visual Studio Code: End-to-End Editing and Debugging Tools for Web Developers
Jump Start Git, 2nd Edition