2

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

IvanRF
  • 7,115
  • 5
  • 47
  • 71
therealsix
  • 656
  • 2
  • 7
  • 16
  • possible duplicate of [Will a 302 redirect maintain the referer string?](http://stackoverflow.com/questions/2158283/will-a-302-redirect-maintain-the-referer-string) – Marc B Aug 31 '11 at 15:29
  • 2
    One problem is that you should not use relative paths for a `Location:` header. [RFC2616](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30) states about the location header: `The field value consists of a single absolute URI`. You MUST use the absolute path of the new location. You can calculate this dynamically from `$_SERVER['HTTP_HOST']` and `$_SERVER['PHP_SELF']` e.g. in this case `header('http://'.$_SERVER['HTTP_HOST'].'/error.php')`; – DaveRandom Aug 31 '11 at 15:40
  • Whilst "it does work" for you, it _might_ not work for someone else. Whether the redirect works with a relative path is dependent on the client/browser. Yes, some clients do work with relative paths, but the spec stats that it should be an absolute URI (as @DaveRandom points out), so some clients might not be so accommodating. – MrWhite Aug 31 '11 at 22:25
  • FYI, the server variable is spelled HTTP_REFERER not HTTP_REFERRER. See http://www.php.net/manual/en/reserved.variables.server.php – Justin Jun 26 '14 at 18:30
  • @IvanRF - Not sure why you deciced to edit my question rather than call out the misspelling of REFERER (or rather my misspelling of the misspelling). Maybe this was my problem... https://en.wikipedia.org/wiki/HTTP_referer – therealsix Jan 17 '20 at 15:09
  • @therealsix there were many users marking the misspelling and that was not the point of your question, so let's correct the misspelling :) Also it will be easier to find on search engines when searching for the PHP variable – IvanRF Jan 17 '20 at 20:19

3 Answers3

4

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

echo $_SERVER['HTTP_REFERER'];
Subash
  • 7,098
  • 7
  • 44
  • 70
MrWhite
  • 43,179
  • 8
  • 60
  • 84
4

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.

animuson
  • 53,861
  • 28
  • 137
  • 147
  • Is it possible to change call so that it uses some other http code that would send the http_referer? This would still be a lot of changes, but a much simpler one. – therealsix Aug 31 '11 at 16:32
  • Could you pass the referrer in URL? Either way, it sounds like it might be a global search and replace? – MrWhite Aug 31 '11 at 22:28
-1

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!

Sharlike
  • 1,789
  • 2
  • 19
  • 30
Hadi77
  • 127
  • 8