简体   繁体   中英

When REQUEST_URI can be empty?

I was creating router for my PHP website and came across such a method :

public function getURI() {
   if (!empty($_SERVER["REQUEST_URI"])) {
      // do some stuff and return the result
   }
}

I figured out even if I request 'example.com' - $_SERVER["REQUEST_URI"] is not empty (it's '/' )

The questions are:

Why do I need to check whether the $_SERVER["REQUEST_URI"] is empty?

When REQUEST_URI can be empty?

In a HTTP call, $_SERVER["REQUEST_URI"] is never empty, because it's a part of the HTTP protocol.

If this function is called in CLI, $_SERVER["REQUEST_URI"] could be empty.

edit

Or, as pointed by @ArtisticPhoenix, it can be changed by the user unset($_SERVER["REQUEST_URI"]); or $_SERVER["REQUEST_URI"]="foo";

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