11

What’s the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? How do I use them?

When I run print_r($_SERVER), PATH_INFO and ORIG_PATH_INFO are not present in the array. Why not? How can I enable them?

I have read the PHP manual on them, but still don’t understand them.

TRiG
  • 10,148
  • 7
  • 57
  • 107
zhuanzhou
  • 2,409
  • 8
  • 33
  • 51

4 Answers4

17

The PATH_INFO variable is only present if you invoke a PHP script like this:

http://www.example.com/phpinfo.php/HELLO_THERE

It's only the /HELLO_THERE part after the .php script. If you don't invoke the URL like that, there won't be a $_SERVER["PATH_INFO"] environment variable.

The PORIG_ prefix is somewhat uncommon. PATH_INFO is a standard CGI-environment variable, and should never be prefixed. Where did you read that? (There were some issues around PHP3/PHP4 if you invoked the PHP interpreter via cgi-bin/ - but hardly anyone has such setups today.)

For reference: http://www.ietf.org/rfc/rfc3875

mario
  • 144,265
  • 20
  • 237
  • 291
  • I think it's meant to be `ORIG_PATH_INFO`. It seems some server configurations create this instead of `PATH_INFO`. – Phil Apr 12 '11 at 02:31
  • Then it's pretty certainly a CGI handler setup. PHP would use the PATH_INFO over SCRIPT_FILENAME in some settings, so the server is likely configured to use an alternative name for path_info. – mario Apr 12 '11 at 02:38
  • https://www.php.net/manual/en/reserved.variables.server.php: `ORIG_PATH_INFO`: > Original version of 'PATH_INFO' before processed by PHP. –  Mar 30 '22 at 12:00
6

try this :

$path_info = !empty($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : (!empty($_SERVER['ORIG_PATH_INFO']) ? $_SERVER['ORIG_PATH_INFO'] : '');
j0k
  • 22,600
  • 28
  • 79
  • 90
dhamaso
  • 61
  • 1
  • 1
2

Prior to 5.2.4, PATH_INFO was apparently broken (not set) in the default configuration. Perhaps that's it.

https://bugs.php.net/bug.php?id=31892

The PHP manual says that ORIG_PATH_INFO is:

Original version of 'PATH_INFO' before processed by PHP.

Reference:
http://php.net/manual/en/reserved.variables.server.php

MrWhite
  • 43,179
  • 8
  • 60
  • 84
Alan
  • 21
  • 1
2

PATH_INFO and ORIG_PATH_INFO are rarely used. These refer to anything in the request path (the part of the URL from the first / on) that comes after the name of the file, and the query string. Generally, you won't have a PATH_INFO in a URL.

I am guessing you mean ORIG_PATH_INFO and not PORIG_PATH_INFO. The path info may be manipulated by things like mod_rewrite and PHP scripts themselves. ORIG_PATH_INFO is the PATH_INFO as it was in the original request, before any rewriting or other manipulation was done to the string.

Wige
  • 3,788
  • 8
  • 37
  • 58
  • 9
    `PATH_INFO` is used in both Symfony and ZF so I wouldn't call it "rarely used" – Phil Apr 12 '11 at 02:33
  • 1
    Just to add to this as I needed to use `PATH_INFO` and got a bit misled by `PATH_INFO` apparently not being used that much. Based on WordPress using this internally: https://github.com/WordPress/WordPress/blob/4.4.4/wp-includes/class-wp.php#L155. It's fair to say it's used [an awful lot](http://www.opensourcecms.com/general/cms-marketshare.php) so use it if you need to! – Josh Davenport-Smith Aug 04 '16 at 15:08
  • "...that comes after the name of the file, and the query string." - Note that PATH_INFO comes _before_ the query string, not after it. – MrWhite Nov 30 '16 at 20:06
  • `ORIG_PATH_INFO` is also used in Agavi (AgaviWebRequest.class.php). – Kamafeather Oct 19 '21 at 21:56