1

Let say I have redirect rule on url example.com which redirect to newurl.net. How can I get original url on newurl.net in order to be used and handled with some details or just to be printed on new destination.

Thank anyone for help.

ross80
  • 23
  • 4
  • 3
    Possible duplicate of [How do you get the 'referer' header in PHP?](https://stackoverflow.com/questions/1031162/how-do-you-get-the-referer-header-in-php) – Andrei Lupuleasa Apr 12 '19 at 13:11
  • The referrer is unreliable, you might not get one at all, or a faked/manipulated one. If you want this to work properly, then you need to put the info you want to transport into the URL you are redirecting to to begin with, so that you can read it from there at the target. – 04FS Apr 12 '19 at 13:22
  • So, the only way possible will be to pass a parameter (i.e. example.com?dom=newurl) and $_GET['dom'] to retrieve the url – ross80 Apr 12 '19 at 13:26
  • Parameter or session seems like your best choice – Dylan KAS Apr 12 '19 at 13:41

1 Answers1

1

If it's not external You can use

$_SERVER['HTTP_REFERER']

And it will give you the referer which is what you want.

See Acquiring referral after a PHP redirect for more informations.

But if you are trying to get to another website, the browser will overwrite the headers so you need to do it some other way.

You can store it in session.

//Save it in your first website
$_SESSION['REFERER'] = $_SERVER['HTTP_REFERER'];

And then in the other website :

//Use it in the other
$referer = '';
if (isset($_SESSION['REFERER'])) {
    $referer = $_SESSION['REFERER'];
    unset($_SESSION['REFERER']);
}

You could also pass the referer as a parameter:

header('Location: http://newurl.net?original_referer=' .$_SERVER['HTTP_REFERER']);
Dylan KAS
  • 4,840
  • 2
  • 15
  • 33
  • I think this will not work as will get server informatin only regarding new location newurl.com – ross80 Apr 12 '19 at 13:29
  • You may want to store it session then or with a parameter then, – Dylan KAS Apr 12 '19 at 13:33
  • The point is that the original url is only domain, no hosting. Starting point will be example.com redirect to newurl.com. Do not have host on example.com. – ross80 Apr 12 '19 at 13:35