Setting per directory PHP configuration values

Last week I wrote about the different location in Windows where php.ini is loaded from. The values set in your php.ini configuration file are global and will be set for every PHP environment on your server. What happens though if you want to configure different PHP environment settings for each site on your server, or even different PHP settings for directories within a site? This can be done with the ini_set() function, but as not all of us are coders or have the time to work code modifications in there is another option; the registry, which is the method which we'll be looking at in this article.

Back in the PHP4 days there was no way in which to configure anything but global PHP directives when using IIS. These days with PHP5 this is possible by configuring the HKLM\SOFTWARE\PHP\Per Directory Values registry value which is a really quick and easy way to configure your PHP_INI_USER values. With this registry setting you are able to specify different PHP directives for any and all directories within your websites.

To do this simple create a key in your registry using regedit under HKLM\SOFTWARE\PHP\Per Directory Values using the structure of your directory path and then create a string value using the name of the PHP directive you wish to alter. For example, if you wish to set the memory_limit PHP directive to a value of 24M in a directory located in X:\path\to\php you'd do the following;

      Create a key with the path of HKLM\SOFTWARE\PHP\Per Directory Values\x\path\to\php
      Create a string value with the name memory_limit

      Set the string name value to 24M

Using this method is really useful as it allows you to really tie down the security on your global values and you only have to modify them for specific PHP applications as needed. If you have any comments or questions feel free to comment here or post in the forums.


Average rating
(1 vote)
|
Submitted by Dominic Ryan on Mon, 2008-01-14 23:29.
Anonymous | Tue, 2008-01-22 05:40
Anonymous's picture

How can we change the registry on server. if it can be done through ssh then can you please tell us the step by step process

Brashquido | Tue, 2008-01-22 10:04
Brashquido's picture

If you don't have RDP access, then SSH/Telnet will be about the only way you'll be able to do it. However, if you are using a shared hosting account I very much doubt you'll have access to modify the registry. Anyway, have a look at the following link for resources on modifying the registry via the command line;

http://www.ss64.com/nt/regedit.html
----------------
Dominic Ryan
3 x Microsoft IIS MVP, MCSE, MCSA
IIS Aid owner/webmaster

Anonymous | Tue, 2008-03-04 21:41
Anonymous's picture

I was able to add the entry into the registry and when I ran the phpinfo.php file it shows the changes, but then when I go run the page that requires register_globals it still executes as though they are "off".

BTW: I set the register_globals to "on"

Any ideas?

Brashquido | Wed, 2008-03-05 01:42
Brashquido's picture

Aha! Seems you've struct the limitation of this method. At least the limitation when used with IIS. The key here is that this method only allows you to change PHP_INI_USER values. I should have made this clearer in the guide, however the full extent of this limitation didn't even become clear to me until after I wrote it. If you have a look at the php.ini directives you'll see a colum titled "changable". Only the directives listed with either "PHP_INI_USER" or "PHP_INI_ALL" can be used with the method detailed in this guide. I originally though that directives listed using "PHP_INI_PERDIR" could also be used as they can with Apache, however this does not seem to be the case as despite been reported back in 2005 and again by myself earlier this year. Sorry I couldn't have any more positive for you.
----------------
Dominic Ryan
4 x Microsoft IIS MVP, MCSE, MCSA
IIS Aid owner/webmaster

Post new comment

  • Use <!--pagebreak--> to create page breaks.
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use BBCode tags in the text, URLs will automatically be converted to links.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Copy the characters (respecting upper/lower case) from the image.