Source File Auto-Detection Via $src_name
2005.10.12 21:47
The most critical function Templation must perform is to determine which file called the driver script so it can open that file and pull out the content. For security purposes, we assume the source file must be underneath DOCUMENT_ROOT. The output of phpinfo() on a typical web server turns up numerous variables that can be appended to DOCUMENT_ROOT. PHP_SELF seems the most obvious solution and works in a lot of cases. Not so fast though!
PHP_SELF fails under at least one common condition: mangling the URL via mod-rewrite will not affect PHP_SELF. Thus PHP_SELF only works for physical filename to URL mappings. REQUEST_URI turns out to suffer from the same issue. The semantically correct variable is SCRIPT_NAME, which is what Templation uses by default.
Unfortunately this too is subject to error, as is demonstrated by Dreamhost's default PHP configuration. With PHP running as CGI, SCRIPT_NAME is always /cgi-system/php.cgi rather then the requested page. In fact, on Dreamhost there is only one variable that works: PATH_INFO, which is a gross misuse of that variable.
It's one thing to look at the list of variables and pick one out, but it gets a lot trickier when you consider the possible presence of query strings, (real) path info, rewrites, aliases, and any number of custom web server configuration scenarios. SCRIPT_NAME should always work, but it doesn't. When installing Templation you should be aware that you may need to configure $src_name right up front. Future versions will hopefully offer better auto-configuration in this department.


