PHP configuration/setting changes via .htaccess and PHP

PHP has various configurable options to enable, disable or limit specific features. Most of the PHP related problems a web developer comes into is due to these limits especially if developing for multiple servers as each server will be configured differently. This is a common problem with shared hosts as you do not have the ability to adjust the PHP configuration directly leaving the developer having to adjust their code to work around the server’s limits or abandoning their host.

PHP.INI
This is the main file in which all the PHP settings are stored, on a shared host you will normally not have access to this file and so alternative methods are required to overcome limits.

Why can’t I change settings on a shared host?
As the name implies it is a shared host meaning it’s not only your website hosted on that server, by adjusting the PHP settings you could accidently break another person’s website by disabling a module they require or increasing/decreasing a limit they rely on. This would be the main reason shared host providers will not allow you to adjust the PHP settings, the other is security most hosts don’t want to allow a PHP script to run for 60 seconds or more due to server resources such as CPU, memory and internet usage, if the PHP configuration was set to loosely someone could easily modify one of their PHP scripts to crash the web server via an infinity loop.

Work Around?
There is various workarounds to overcome these limits the easiest would be if you had access to the php.ini file or managed the server yourself. Some hosts provide features where each of their clients gets a separate php.ini file, this allows the client to customer the PHP settings to their liking and the changes only affect their own websites (some limits may still be in place such as maximum execution time if your settings are too high a global setting will takes its place to stop malicious usage).

.htaccess (Apache) workaround
If you don’t know what .htaccess file is then please read this post.

This is a simple method but will not work on all hosts due to restrictions (PHP also has to be installed as a Apache module), for this to work your host needs to give you specific apache privileges which are.

or

You may be able to add either of the following lines to your .htaccess file to manually give yourself the privileges required. The next step is to add the lines of code which will change the PHP settings. The following line is the command we will be using to override the PHP setting.

The section called name is the option you wish to change and value is the value you want to change it to, if you are wanting to adjust a setting which has a value of either 0, 1, yes, no then you must use the following line instead.

A list of available commands can be found here.

http://www.php.net/manual/en/ini.list.php

In the table there is a column called “Changeable” if the value of this column for the setting you wish to change is not either “PHP_INI_ALL” or “PHP_INI_PERDIR” then you will not be able to adjust the setting using this method. Here is an example of my .htaccess file which will increase my max upload file size.

The following will adjust my upload_max_filesize limit to 100M instead of the default value which is 2M, my options are surrounded by a IF statement which basically checks if PHP5 is in use if so then it runs the command to adjust the settings.

PHP workaround
PHP comes with the ability to override some of its predefined settings but just like the other over rights it depends entirely on the setup of the web server and may not work. The overrides for PHP work exactly the same as the .htaccess ones explained above but we use the following command to adjust the settings.

The documentation for this function can be found here, it is used just like any normal line of PHP code here is an example of some PHP code where I have increased the file upload limit to 100M.

A list of all PHP settings is available here but only the settings which have PHP_INI_ALL, PHP_INI_PERDIR or PHP_INI_USER can be changed via the ini_set command.

Extra Links

http://www.php.net/manual/en/ini.list.php

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.