Undefined index: PATH_INFO

Asked

Viewed 1,142 times

9

I would like to recover the URL using $_SERVER['PATH_INFO'] and I had a nice surprise :

Undefined index: PATH_INFO

On the contrary, using the $_SERVER['REQUEST_URI'] everything works perfectly.

What should be the origin of this error ? What is this about ?

Note :

  • I’m on a localhost : 127.0.0.1/Tuto/Site/application/index.php;

  • I called the variable $_SERVER['PATH_INFO'] in the index.php file

2 answers

9


In this case there is no information to generate the PATH_INFO of $_SERVER, because, it rescues the additional information in a URL from a script , disregarding in question the Query String (information found in this array $_SERVER['QUERY_STRING']).

The $_SERVER second site php.com is a array containing information such as headers, paths, and script locations.

To rescue this PATH_INFO must have that kind of structure (layout) of URL:

http://localhost/script.php/some/all/run?g=10

commando:

echo $_SERVER['PATH_INFO'];

exit:

/some/all/run

Observing: if by chance the Query String come before also does not work have to follow this structure for the environment variable to be loaded.

Example: http://localhost/script.php?g=10/some/all/run, This does not work for loading $_SERVER['PATH_INFO'].


Maybe that’s not what you need, and it causes confusion with $_SERVER['REQUEST_URI'] that brings more information about the URL executed.

Like the command echo $_SERVER['REQUEST_URI'] the exit is:

/script.php/some/all/run?g=10

bringing complete information from URL for the execution of a particular script .


References:

  • 1

    Thanks... Happy New Year @Virgilio!

  • 1

    @Andrépka equally, a great year to all...

7

If you want the equivalent of PATH_INFO to take the path of path, the solution is:

$_SERVER["PHP_SELF"]

The PATH_INFOonly brings the path, without query string and other information, then the PHP_SELF is the nearest substitute.


Understanding the reason

The PATH_INFO is not a PHP variable, it is an Apache variable that is passed to PHP (more exactly, it is a CGI specification, but Apache passes even when PHP is installed as a module). So it’s not even portable, and it depends on some conditions even in Apache.

Basically it is used when you have a path that goes beyond the file being accessed, be it a /meu.php/caminho, or a /caminho/completo that is being provided by a PHP file indicated in a .htaccess, route or even as root document of the server (instead of root folder).

Already the PHP_SELF is an indication of the script itself being executed, so in your case it will reflect the path that triggered the PHP itself in use. Important to understand the difference so you can choose which to use in situations where the results of the two values are different.

More details here:

http://httpd.apache.org/docs/2.2/mod/core.html#acceptpathinfo

https://tools.ietf.org/html/rfc3875#Section-4

  • Happy New Year ... Thanks !

  • 1

    Equally, happy holidays and congratulations to you and all readers.

  • 1

    Answered what I was going to say I quote "So it’s not even portable, and it depends on some conditions even in Apache", there are better ways than PATH_INFO, I say by own experience +1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.