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";