15

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... ?

1
  • 7
    Use var_dump($_SERVER) and see for yourself. Commented Dec 19, 2012 at 8:32

3 Answers 3

16

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;
Sign up to request clarification or add additional context in comments.

1 Comment

Quicker method would be: <code>$httpReferer = $_SERVER['HTTP_REFERER'] ?? null;</code>
4

$_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.

2 Comments

what could be the reliable alternative to $_SERVER['HTTP_REFERER'] ?
@Dheeraj There is no reliable alternative. The $_SERVER['HTTP_REFERER'] PHP superglobal simply reports the value of the underlying Referer HTTP request header. It is the HTTP request header that is unreliable. The referring document is not passed in any other way.
0

$_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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.