Oto Shavadze
Oto Shavadze

Reputation: 42753

Result from $_SERVER['HTTP_REFERER'], when referer header is not sent to server

When the browser sends header info to the server, $_SERVER['HTTP_REFERER'] should give us the previous page URL right?

What returns from $_SERVER['HTTP_REFERER'], when header info is not sent to server? empty string? false? null? or... ?

Upvotes: 15

Views: 54591

Answers (3)

wau
wau

Reputation: 830

$_SERVER is a global array variable, and the referrer value is an element of the array with key HTTP_REFERER. If is no referrer header is sent by the browser, then the element is simply missing from the array. You can check whether an array has an element with array_key_exists; in this case:

array_key_exists('HTTP_REFERER', $_SERVER)

Upvotes: 0

MrWhite
MrWhite

Reputation: 45829

If the HTTP referer request header is not sent then the $_SERVER['HTTP_REFERER'] is probably not set, although it could be an empty string. Whether it is set or not in this case could depend on the server.

As with all HTTP request headers, check for its existence when reading:

$httpReferer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : null;

Upvotes: 16

Shoe
Shoe

Reputation: 76240

$_SERVER['HTTP_REFERER'] is not really reliable because particular setting on the user browser could break it. But yes it should contain the previous URL and it will return empty string or NULL when headers are not sent, depending on the server configuration.

Upvotes: 4

Related Questions