Redirect Page but KEEP HTTP_REFERER working ???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hookgr
Forum Newbie
Posts: 2
Joined: Wed Jun 28, 2006 6:46 pm

Redirect Page but KEEP HTTP_REFERER working ???

Post by hookgr »

Hello guys,

I am on page a.php and need to send user to b.php.

If I give him an href to click, on b.php I can get as $_SERVER['HTTP_REFERER'] the http://domain/a.php value.

If I send him to b.php with header "Location" or "Refresh", b.php has BLANK / EMPTY HTTP_REFERER...


How can I automatically send user to b.php but keep HTTP_REFERER working ???
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Untested

a.php

Code: Select all

session_start();

$_SESSION['referrer'] = $_SERVER['HTTP_REFERER'];

// header redirect...
b.php

Code: Select all

session_start();

$referrer = $_SESSION['referrer'];
and yet another way...

a.php

Code: Select all

header("Location: http://sitename.com/b.php?referrer=" . $_SERVER['HTTP_REFERER']);
b.php

Code: Select all

$referrer = $_GET['referrer'];
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

It is bad practice to rely on http_refferer to exist, as it likely won't for a large proportion of the time. Might want to look into alternatives, such as saving the "page name" during each page access, and checking against that on any new page hits.
hookgr
Forum Newbie
Posts: 2
Joined: Wed Jun 28, 2006 6:46 pm

Post by hookgr »

i dont mean how i can pass between the two scripts the variable referer or the data, i know about sessions and lot other methods.

the problem is what b.php itself detects as referer...

when you do header Location redirect, REFERER field is blank, and i want it to appear as if user has clicked a link to go to b.php
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

The PHP manual says:
Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted.
"... it cannot really be trusted."!!!

What you want to do with the 'HTTP_REFERER' info???
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

hookgr wrote:i dont mean how i can pass between the two scripts the variable referer or the data, i know about sessions and lot other methods.

the problem is what b.php itself detects as referer...

when you do header Location redirect, REFERER field is blank, and i want it to appear as if user has clicked a link to go to b.php
The answer: No. Not possible.

astions has provided you with alternatives, if they are not good enough, then what you seek is not possible.
Post Reply