PHP Server API Differences
A useful table: Apache and IIS PHP $_SERVER Superglobal Comparision.
If youâve written code thatâs had to run under more than one web server environment (even PHP as CGI vs. PHP as module under Apache), youâve probably run into headaches like $_SERVER[âSCRIPT_FILENAMEâ] being unavailable â Server APIs (SAPIs) vary.
Ran into the link table a few days ago and it illustrates potential problems nicely (I assume that the IIS column was created from PHP running as an ISS âmoduleâ, not as a CGI executable). Perhaps the two most critical are $_SERVER[âSCRIPT_FILENAMEâ] and $_SERVER[âREQUEST_URIâ], both of which youâre likely to want if youâre doing anything âframeworkyâ but are are generally only available to PHP running as an Apache module.
The link doesnât quite raise the full story though. As I remember, with PHP as a CGI under Apache, $_SERVER[âSCRIPT_FILENAMEâ] is the path to the CGI executable, not the script it is executing â that relates to a request for a PHP constant called __MAIN__ to identify the script where execution began.
Otherwise the various HTTP_ variables in $_SERVER array could vary per-request â these tell you information about the incoming HTTP request. Thereâs also the informational X_HTTP_ that sometimes show up, such as X_HTTP_FORWARDED_FOR â a convention amongst proxy servers. None of these you should trust by the way.
Thereâs further fun to be had with Apache 2 and $_SERVER[âPATH_TRANSLATEDâ] â see here. The PHP function php_sapi_name() is useful for determining which environment your scripts are running under. Side note here â believe it returns âapacheâ when PHP is running as an module under Apache 1.x and 2.x.
And if you were thinking â âWho cares? Iâll just rely on good old $_SERVER[âPHP_SELFâ] right?â then you should read this â searching Google for âform tutorial PHP_SELFâ is the stuff of nightmares.
Some work has been done to provide SAPI-specific functionality to help âunderstandâ parts of the environment (e.g. Apache and NSAPI allow you to get more information about incoming requests) but, IMO, the bottom line is thereâs a minefield if youâre writing code that should run under more than one SAPI.
Right now donât have any particular answers or conclusions. PHP is a cross platform technology, not platform independent. The #1 problem, to my mind, is there doesnât seem to be any definitive documentation of where the differences lie. Thereâs a bunch of tips attached to the manual on predefined variables but most of these are of the âthis worked for meâ kind. In a perfect world all of this might be hidden behind an API that magically works it all out, but Iâm not aware of anything that offers this.
Thoughts / opinions / links etc.?