How to overwrite php.ini settings using .htacess

Couple of days back I was working on a project for one of my clients. During that I came across a situation where in I had to set display_errors in php.ini to On for the particular client’s application. But in my WebServer I preferred to keep display_errors to Off. I did not want my original php.ini file to change and let the effect flow to all sites hosted in my WebServer. In such situations we always have alternatives method to make our task easier and achieve what we wish.

So before I proceed with the method to achieve the result, let me introduce you to php.ini. It is the configuration file used to customize the behavior of PHP in runtime. When PHP is started it looks for the php.ini file and load the desired settings. There is lot of confusion among fellow webmasters about the location of original php.ini file in the webserver. Depending upon the configuration of the webserver php.ini file can be found in any of the following locations:

/etc/php.ini

/etc/php/php.ini

/etc/php5/php.ini

The best way to find where the php.ini file is by using the phpinfo() function. Create a file called info.php and paste the following code inside it. Put this file in the root directory of your website.

<?php phpinfo() ?>

When you execute this file through web browser it will show you the different php settings including the location of php.ini file.

.htaccess (Hyper Text Access) is a directory level configuration file primarily used by Apache webserver, that allows decentralized management of web server configuration. You need to create the .htaccess file in the document root of your website through ftp as Windows will not allow you to create/rename files to .htaccess (or any name starting with dot)

As we are familiar with php.ini and .htaccess by now, let’s look at the method to overwrite the php.ini settings for each script or website. When we are on a shared hosting and we do not have access to the global php.ini file, at those moments this method of overwriting php.ini settings through .htaccess will definitely come as a rescue.

– If you do not have an .htaccess file in your website document root create one through ftp

– In the .htacess file put the required php settings in the following format:

<php_flag> <settings_name> <Settings_value (On|Off)>

<php_value> <settings_name> <Settings_value>

The php_flag is used to set the directive to boolean values, for example for the directives like display_errors or set_register_globals which takes On|Off as values

The php_value is used to set the directive to actual values like upload_max_filesize which takes values like 10M or 20M

After you make changes in .htaccess file the sample htaccess content should look like this:


### Custom php.ini setting

php_flag display_errors On

php_value  max_upload_filesize  10M

Note: this .htaccess method only works if you are using Apache webserver.

email


Ajay is the editor and webmaster of Techie Zone . He is also a Wordpress expert and provides Wordpress consultancy and web services. Recently he has launched his maiden start-up Qlogix Solutions. You can follow him in twitter @ajaykumarmeher


Techie Zone is looking for Guest Blogger. You can write for us to get exposure and backlink. Read here for more info.

Leave a Reply

Powered ByQlogix Solutions