I'm seeking a portable way to receive the (handy) $_SERVER['PATH_INFO']
variable.
After reading a while, it turns out PATH_INFO
is originated from CGI/1.1, and my not always be present in all configuration.
What is the best (mostly security-wise) way to get that variable - apart from extracting it manually (security concern).
I think here is a trick to get "path_info" in other way:
For example, access to a URL like: http://somehost.com/index.php/some/path/here, the value of
$path_info
would be:"/some/path/here"
It worked for me in various apache servers running on windows and linux, but I'm not 100% sure if it's "safe" and "portable", ovbiously I don't test it in "ALL" servers configs, but appears to work...
Well, I'm (almost) sure that without making use of the
$_SERVER
superglobal keys, providing a alternative way to figure outPATH_INFO
is just impossible, that being said lets first list all of the $_SERVER keys that we may possibly use:We obviously need to ignore the last two. Now we should (I don't know this for a fact, I'm just assuming because you said so) filter all the keys that exist in the link you provided (which BTW is offline ATM), that leaves us with the following keys:
Regarding your comment to Anthonys answer:
I'm running LightTPD/1.4.20-1 (Win32) with PHP 5.3.0 as CGI,
cgi.fix_pathinfo = 1
and$_SERVER['REQUEST_URI']
is very available to me, I also remember using that same variable back in the days when no one usedmod_rewrite
so my honest humble guess is that you're plain wrong in this point. Regarding theSCRIPT_FILENAME
key I'm unable to test that one out ATM. Still, if we close our eyes really hard and believe that you're right that leaves us with only one variable:I'm not trying in being harsh here (and I still believe that there are more solutions) but if
PHP_SELF
is the only key you want us to work with (assuming there are no impositions onPHP_SELF
itself) there is only one solution left:This function should work, however there may be some problems using the
__FILE__
constant since it returns the path to the file where the__FILE__
constant is declared and not the path to the requested PHP script, so that's why the $whatToUse is there for: sou you can replace it with'SCRIPT_FILENAME'
or if you really believe in what you are saying, just use'.php'
.You should also read this regarding why not to use
PHP_SELF
.If this doesn't work for you, I'm sorry but I can think of anything else.
EDIT - Some more reading for you:
REQUEST_URI
is Apache specific?)PHP_SELF
vsPATH_INFO
vsSCRIPT_NAME
vsREQUEST_URI
I didn't see the comments or the link before posting. Here is something that might work, based on what the page referenced above gives as CGI-derived variables:
you could try
It depends on the definitions for "portable" and "safe".
Let me see if I understood:
1) You are not interested on CLI:
2) You want to have PATH_INFO in all OS + HTTP server + PHP combination:
Hmmm... PHP_INFO, in the $_SERVER array, is provided by PHP to a script in execution only under certain conditions, depending on the softwares mentioned above. It is not always available. The same is true for the entire $_SERVER array!
In short: "$_SERVER depends on the server"... so a portable solution can't relay on $_SERVER... (just to give one example: we have a tutorial to set up PHP/CGI $_SERVER variables on NginX HTTP server at kbeezie.com/view/php-self-path-nginx/)
3) Despite what was mentioned above, it worths mentioning that if we somehow have the full URL that was requested available as a string, it is possible to obtain the PATH_INFO from it by applying regular expressions and other PHP string functions, safely (also validating the input string as a valid URI).
So, provided that we have the URL string... then YES, WE HAVE a portable and safe way to determine PATH_INFO from it.
Now, we have two clear and focused implementation issues:
Among several possibilities, here is a possible approach:
How to obtain the URL?
1) With your deep and comprehensive knowledge about each HTTP server + OS + PHP version combination, check and try each possibility to obtain the URL from the $_SERVER array (verify 'PHP_SELF', 'QUERY_STRING', 'SCRIPT_FILENAME', 'PATH_TRANSLATED', 'SCRIPT_NAME', 'REQUEST_URI', 'PATH_INFO', 'ORIG_PATH_INFO', 'HTTP_HOST', 'DOCUMENT_ROOT' or whatever)
2) If previous step failed, make the PHP script return a javascript code that sends "document.URL" information back. (The portability issue transfered to client-side.)
How to obtain the PATH_INFO from the URL?
This code linked here does this.
This is my humble opinion and approach to the problem.
What do you think?
Edit: without SCRIPT_NAME, and assuming you have DOCUMENT_ROOT (or can define/discover it yourself) and assuming you have SCRIPT_FILENAME, then:
Also @ Anthony (not enough rep to comment, sorry): Using str_replace() will match anywhere in the string. It's not guaranteed to work, you want to only match it at the start. Also, your method of only going 1 slash back (via strrpos) to determine SCRIPT_NAME, will only work if the script is under the root, which is why you're better off diffing script_filename against docroot.