I cannot use the global var $_SERVER['PATH_INFO']

Asked

Viewed 81 times

2

I was trying to redirect, picking up the url path using var global $_SERVER['PATH_INFO'], but without success.

In giving print_r($_SERVER);, I saw that PATH_INFO was no longer listed.

ex.:

127.0.0.1:8080/edsa-Site/index.php

In this case I wanted to take the "/index.php" part. How can I do this, and if possible get only "index" ?

1 answer

2

The variable PATH_INFO is even common in Apache, but not every server will own it, because it is not always configured (depending on each server), basically you have to configure it manually, in your case you are probably using php-built-in-server, be the case or not to get the path can use the $_SERVER['REQUEST_URI']

The "problem" of $_SERVER['REQUEST_URI'] is that it returns querystring as well, for example if the URL is like this:

http://127.0.0.1:8080/edsa-Site/index.php?foo=bar

Will return this:

/index.php?foo=bar

And of course, this is much more than the way to solve this can use explode, preg_replace, substr+strpos, but my suggestion is to use something simpler, the strtok, getting something like:

$requri = strtok($_SERVER['REQUEST_URI'], '?');

var_dump($requri);
  • I did the test on the built-in php server, and it worked, now using Devserver 17 I can no longer use PATH_INFO. Is there any way to configure the same one to use? I want to be able to do some tests, but I need to use a database, and with the server embedded in php I can’t make use of a database as far as I know.

  • @Medivh devserver17 is not even a server, it is a package of programs recommend that you read https://answall.com/a/115690/3635 ... do not need to use PATH_INFO, use the code that is in the answer.

Browser other questions tagged

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