ross80
ross80

Reputation: 23

How to retrieve original url in a redirected page using PHP

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.

Upvotes: 1

Views: 1528

Answers (1)

Dylan KAS
Dylan KAS

Reputation: 5693

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']);

Upvotes: 1

Related Questions