0

I redirect to a URL in PHP using the following code:

header('Location: http://site.com/target.php');

How do I get the URL that redirected to the target? I tried this code but it's not working:

echo $_SERVER["HTTP_REFERER"];

Thanks

Michael Samuel
  • 3,820
  • 12
  • 45
  • 85

5 Answers5

2

Try this:

<?php echo "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>
Sumit Bijvani
  • 8,154
  • 17
  • 50
  • 82
0

Sumit's Code gave you the current URL and you can use it using session :

$current_url="http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$_SESSION['url']=$current_url;
header('Location: http://site.com/target.php');

Get the URL on the Page :

echo $_SESSION['url'];
Harshal
  • 3,562
  • 9
  • 36
  • 65
0

You can't rely on the REFERER header, because some browsers will not send it as a security precaution.

If you need to know what URL they requested originally, you could pass it as a query parameter in the Location URL. Alternatively, you could store it in a database on the server and pass a key into the database as part of the query string, or similarly store it in a session on the server and pass the session ID as part of the query string.

You may get a more useful answer if you can explain what you're trying to do exactly. Why do you need to know what address they came from, and why do you need to redirect to a different address? Also, are the two addresses on the same website (i.e., the same domain name)?

brianmearns
  • 9,581
  • 10
  • 52
  • 79
0
<?php echo $_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'] ; ?>

this link will really help you $_SERVER

mostafa khansa
  • 302
  • 1
  • 6
-1

Each time you visit a page store that in $_SESSION, so whenever you need the value of the previous page you can view it easily.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • Why should anybody know where were you before you have visited my site? It is not working in that case? Yes, it is not. But why should that work? If you are buying food in a restaurant, they might be interested what did you eat. But why should they know where and what have you eaten before? Isn't that called spying? Isn't that supposed to be a bad thing? So if it is, then why do we consider not supporting a bad thing a problem? – Lajos Arpad Sep 04 '13 at 12:21
  • @Lajor "Why should that work" ... why not ask the OP, he's the one wanting a solution. Not me. – Adrian Wragg Sep 04 '13 at 12:24
  • The op never specified that he wants to track foreign pages. You are assuming. – Lajos Arpad Sep 04 '13 at 12:38
  • "why not ask the OP, he's the one wanting a solution." It was not specified that foreign pages are included into that solution. This is an assumption. – Lajos Arpad Sep 04 '13 at 12:42
  • I'm sorry that you seem to be having difficulty understanding my simple comment. Unfortunately, I have neither the time nor the crayons to explain it further. – Adrian Wragg Sep 04 '13 at 14:31
  • Wow, instead of trying to prove your right you are starting to "doubt" my capabilities. Typical trolling. I have shown you where was your assumption, but it seems it hurts to prove your right or recognize that you were wrong. Good luck for buying your crayons, as I don't buy your comment. – Lajos Arpad Sep 04 '13 at 15:30