Question

I have been looking for a while here and can't find an answer to my specific question.

In 100's of places on my site I am redirecting to an error page via a header-location redirect.

header("Location: /error.php");

What I want to do is capture and log the HTTP_REFERER on the error.php page, but for some reason it isn't set. I have seen explanations of why it isn't set with Meta-refresh, but the location header is a 302, so it should be set right? Any thoughts?

Please note: I know the HTTP_REFERER is unreliable, and I know that I can pass the information separately. Neither of which matter in my scenario (unless I want to change all the places where the redirect is called).

Was it helpful?

Solution

Well, the HTTP_REFERER is set by the browser and if the browser chooses not to set it on a 302 redirect, which is shouldn't, then you won't get it. A 302 is a temporary redirect, which means the previous page is temporarily unavailable. Why would the browser want to send information for a page that doesn't exist right now?

Good ways to achieve this:

  • Personally, I have an Errors class that handles this type of stuff, rather than just redirecting straight from the page. This allows you to record all the information you need, even debug information, before the redirect even occurs.
  • Set a session that includes the page information and grab that information on the error page for recording.
  • You can try to set the Referer line using header, but some browsers ignore this and will not send it anyways.

Really there is no way to fix this without modifying the code at each and every place where it's redirected that you also want to record information for.

OTHER TIPS

It is actually HTTP_REFERER (with a single 'R' in the middle) - a misspelling that stuck!

echo $_SERVER['HTTP_REFERER'];

Some browsers haven't support for HTTP_REFERER. you can use isset() function and this is very important that you must use HTTP_REFERER not HTTP_REFERRER!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top