REQUEST_URI is the URI path and query as it was requested. Besides that $_SERVER['HTTP_REFERER']
contains the value of the HTTP request header field Referer if available. So to check whether both contain the same URI path, you can do this:
if (isset($_SERVER['HTTP_REFERER'])) && parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) === parse_url($_SERVER['HTTP_REFERER'], PHP_URL_PATH)) {
// Referer existing and its path is equal to the current requested URI path
}
But the reason for why isset
is used in this case it that the Referer is not always sent. In general, only when the request is caused by following a link or sending a form that header field will be sent by the client. So it is likely that this header field is not set. Besides that, its value can also be forged and thus is not trustworthy like any other information that is coming from the client.
This is also the reason why you shouldn’t use this information to verify the authenticity of a request. Use your own authentication tokens instead.