0

I am redirecting from several pages to dev.php using php header

<?php header(Location: dev.php); ?>

I want to know from where i have been redirected here.

I have tried

<?php
print "You entered using a link on ".$_SERVER["HTTP_REFERER"];
?>

but it dosent work as $_SERVER["HTTP_REFERER"]; only works if you access dev.php using <a href="dev.php">Go to Developer Page</a>

So how can i get the referring URL?

Niklas Wulff
  • 3,497
  • 2
  • 22
  • 43
sudo
  • 906
  • 2
  • 14
  • 32

5 Answers5

2

Try something like below.

<?php header(Location: dev.php?referrer=$_SERVER[PHP_SELF]); ?>

OR

header('Referer: '.$_SERVER['PHP_SELF']);
header(Location: dev.php);

If above methods don't work, You will have to go with sessions.

Techie
  • 44,706
  • 42
  • 157
  • 243
  • `header('Referer: '.$_SERVER['PHP_SELF']);` dosent work and I cant use dev.php to get variable – sudo Jul 22 '13 at 09:06
  • check the updated answer. it should work. set the header before use redirect. – Techie Jul 22 '13 at 09:08
  • yes i have used the updated one earlier first i set the `header('Referer: '.$_SERVER['PHP_SELF']);` then redirected – sudo Jul 22 '13 at 09:14
  • If above methods don't work, You will have to go with sessions – Techie Jul 22 '13 at 09:14
  • The Referer header is a request-level header (see [RFC 1945 section 10.13](https://tools.ietf.org/html/rfc1945#section-10.13)) so setting it in a response has no effect. – Janos Pasztor Jul 22 '13 at 09:28
1

You can pass the variable in GET:

<?php header('Location: dev.php?backurl='.$_SERVER[PHP_SELF]); ?>

And then take it in this way:

$backurl=$_GET['backurl'];
PaoloCargnin
  • 442
  • 2
  • 10
0

You need to add the referer to the header manually:

header("Referer: http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']); // current page
header("Location: dev.php");

You can also do it using CURL. Have a look at an example function for that here: PHP - Referer redirect script

Community
  • 1
  • 1
Adam
  • 1
  • 1
  • I've added a link for a method using CURL. – Adam Jul 22 '13 at 09:21
  • The Referer header is a request-level header (see [RFC 1945 section 10.13](https://tools.ietf.org/html/rfc1945#section-10.13)) so setting it in a response has no effect. – Janos Pasztor Jul 22 '13 at 09:28
0
$_SERVER["HTTP_REFERER"] = 'YOUR_URL';
header('Location: dev.php');
exit(0);
web-nomad
  • 6,003
  • 3
  • 34
  • 49
0

The referer header is rather unreliable, there are antivirus software and firewalls out there that filter it completely. The only thing you can pretty safely rely on are cookies and with them, sessions.

Cookies are stored on the client side, in the browser, therefore can be manipulated, but if you are storing non-critical information, they can be enough.

Sessions nowadays rely on cookies as a means of starting them. A randomly generated hash is stored in a cookie, the corresponding data is stored on the server linked to this hash. When the client requests the next page, this data is retrieved. For more information about sessions read the corresponding section of the PHP manual.

As I wrote, sessions rely on cookies nowadays and you shouldn't do that unless you are aware of the security implications. You can however use URL parameters to pass the session ID along.

On a side note: in order to use cookies (and with them sessions) both the setting and the final URL must be on the same domain. If this isn't the case, you can use request parameters to pass the information along.

Janos Pasztor
  • 1,265
  • 8
  • 16