Use SCRIPT_NAME not PATH_INFO in PHP

You should use

$_SERVER['SCRIPT_NAME']

not

$_SERVER['PATH_INFO']

in PHP. PATH_INFO has been monkeyed with in some versions (and when running as FastCGI?) so sometimes it appears as PATH_INFO and other times as ORIG_PATH_INFO and PATH_INFO does not exist in the $_SERVER array. SCRIPT_NAME seems to have had a far less chequered history. Sometimes I think there could be a new beautiful version of PHP without little gotchas like this, but then that wouldn’t really be PHP would it?

2 Replies to “Use SCRIPT_NAME not PATH_INFO in PHP”

  1. I’m afraid the advice I give here is wrong. On dreamhost, PATH_INFO gives you what you want and SCRIPT_NAME gives “/cgi-system/php.cgi”. There is not foolproof way of always getting the path the user has entered to get to your page. That’s a shame.

  2. And there’s PHP_SELF too. Not sure if that’s any better.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.