views:

1420

answers:

2

I have a PHP script that is redirecting the user via some code like:

header ('Location: http://someurl/somepage.php');

Then in somepage.php I am trying to access $_SERVER['HTTP_REFERER'] to determine where the page request has come from. I find that $_SERVER['HTTP_REFERER'] is empty when a page is called using the header location method.

Is there an alternative method that I can use to redirect the user so that I can still use $_SERVER['HTTP_REFERER'] in the target page.

+2  A: 

One thing you could try: instead of sending the Location header, send back an HTML page with the tag

<meta http-equiv="refresh" content="0;url=http://someurl/somepage.php"&gt;

in the <head> section. I'm not sure if it would fix your problem, though... it really depends on how the browser behaves. The value of $_SERVER['HTTP_REFERER'] comes from the Referer request header which the browser is free to send or not send, at its discretion.

Something else you could do is

header("Location: http://someurl/somepage.php?referer=http%3A%2F%2Fthisurl%2Fthispage.php");

that is, send the referer URL as a query string parameter, then you can access it as $_GET['referer']. This is probably more reliable than relying on the browser to send a referer header.

David Zaslavsky
+1  A: 

The HTTP Referer: header is set by the user agent, and not by the server. I'm not sure that there is anything you can do to ensure that you get a Referer header.

vezult