Undefined index: PATH_INFO

9

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

  

Undefined index: PATH_INFO

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

What should be the source of this error? What is it all 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

asked by anonymous 31.12.2016 / 21:27

2 answers

9

In this case, you do not have information to generate the PATH_INFO of $ _SERVER , because it rescues the additional information in a URL from a script , disregarding the Query String a> (information found on this array $ _ SERVER ['QUERY_STRING'] ).

The $ _ SERVER second php.com site is a array containing information like headers , paths , and script locations .

To redeem this PATH_INFO you should have this type of layout ( URL :

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

command:

echo $_SERVER['PATH_INFO'];

output:

/some/all/run

Note: if by chance

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

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

As the echo $_SERVER['REQUEST_URI'] output command is:

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

Bringing in full information on the URL determined script .

References:

31.12.2016 / 23:11
6

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

$_SERVER["PHP_SELF"]

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


Understanding the reason

PATH_INFO is not a PHP variable, it's an Apache variable that is passed to PHP (more accurately, it's a CGI specification, but Apache passes even when PHP is installed as a module). So, it's neither portable, and 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 supplied by a PHP file indicated in .htaccess , route or even as document root (instead of root folder).

While PHP_SELF is an indication of the script itself being executed, then in your case it will reflect the path that triggered the PHP itself in use. It is important to understand the difference in order to choose which to use in situations where the results of the two values are different.

More details here:

  

link

  

link

    
31.12.2016 / 23:54