简体   繁体   中英

REQUEST_URI set or not set

I have a small bit of sample code to pull in the URI of a page. This page runs on a Linux/Apache installation:

$url = "";
if (!isset($_SERVER['REQUEST_URI'])) {
  echo "request_uri is not set";
  $url = "value could not be set, !isset on REQUEST_URI was true";
} else {
  $url = $_SERVER['REQUEST_URI'];
}
echo "url = $url <br />";

When I pull this page up in the browser, the code appears to have hit the "else" condition and I get the correct URI.

However, when I run this code directly from the command line using "php filename.php" the code appears to hit the if condition, where REQUEST_URI is not set. If I remove the "if" condition, I get the error "PHP Notice: Undefined index REQUEST_URI in..."

This is making me a little crazy! Is there a problem with REQUEST_URI? Why would it return different answers between the browser and the command line? Where can I go on the server to see if this value is actually set and returnable?

Thanks for any help you can provide.

If you call it from the browser you are making a REQUEST to a URI and then the HTTP server is executing the PHP program and telling it what that URI is.

If you call it from the command line, then there is no HTTP REQUEST and there is no URI (nor, for that matter is there a SERVER).

The behaviour you are getting is entirely expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM