Search code examples
phpiisfastcgiisapi

Can I configure environment specific content?


In the same way that you can generate specific content based on browser type is there a way to generate specific content based on the server running PHP without reference to the server or site name?

For example, a way for PHP to automatically detect the environment it was in and configure things like DB connections, ini_set for errors etc. depending if it was a development, ITS, UAT or production environment.

The 2 ways I thought of were to recognise an HTTP header indicating development and QA environments or to have custom properties in php.ini.

I have woken up slightly and found out the php function to read the http headers but php overrides anything I set in the web server and I do not know if they can be set in php.ini at all.

I have no idea if it is possible to add custom values to php.ini but I had a test and ini_get would not find it (I had restarted the web server after changing php.ini of course).


Solution

  • Using FastCGI on IIS you can set Environment variables. They do not seem to be available to $_ENV but can be retrieved with getenv("varname").

    To configure FastCGI environment variables in IIS 5 or 6 you need to edit: C:\%systemdrive%\system32\inetsrv\fcgiext.ini

    For example:

    [Types]
    php=d:\Dev\PHP\php-cgi.exe
    php:1=PHP Site 1
    *=Wildcard Mapping
    
    [d:\Dev\PHP\php-cgi.exe]
    QueueLength=999
    MaxInstances=20
    InstanceMaxRequests=500
    
    [PHP Site 1]
    ExePath=d:\Dev\PHP\php-cgi.exe
    EnvironmentVars=PHPRC:d:\Dev\PHP\,SiteType:Developer
    

    In this instance it is IIS 5 so there is only one site and the site ID is 1 as indicated in line 2 of [Types].

    On IIS 6 you may have multiple sites and the following link tells you how to find the Site ID: http://weblogs.asp.net/owscott/archive/2005/07/29/how-to-find-the-siteid-in-iis5-and-iis6.aspx.

    IIS 7 can be configured via the UI apparently once the Administration Pack for IIS 7 has been installed.