?
Solved

Referer information lost on re-direct

Posted on 2004-08-22
7
Medium Priority
?
341 Views
Last Modified: 2008-03-03
I am doing this
header('location:'.$newurl);

to make a php page do a re-direct to a new url, but when the new page is displayed it dosnt have any referer.

Any ideas on how to make sure referer is always set? Is there a header function to set the referer?

0
Comment
Question by:wildzero
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 27

Expert Comment

by:Diablo84
ID: 11865912
the referer is unreliable anyway so as a general rule avoid using it, in these situations i usually advise using the sessions work around.

first page:

session_start();
$_SESSION['ref'] = $_SERVER['PHP_SELF'];


checking page:

session_start();
if (!isset($_SESSION['ref'])) {
 //referer is not set
}

you can alternatively check the value stored in the session too if you are checking multiple pages, eg.

session_start();
if (isset($_SESSION['ref']) && $_SESSION['ref'] == "/yourreferingpage.php") {
 //referer is set and is valid
}

make sure session_start(); is always at the top of your page
0
 
LVL 10

Author Comment

by:wildzero
ID: 11866113
The page that it is getting sent to is on another server so i can't do sessions. Referers is the best way and it works perfect, but when you re-direct with header('location......... it loses the data

0
 
LVL 27

Accepted Solution

by:
Diablo84 earned 375 total points
ID: 11866404
As far as im aware the information should be retained none the less, even so i still would not advise trusting the referer as it is set client side and can be blocked from being sent so there will be cases where it will not be set, quote from the manual:

"The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. 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."

That said you could use a modification of the above code using cookies instead of sessions, example:

first page:

$ref = $_SERVER['PHP_SELF'];
setcookie("ref", $ref, time()+3600, "/", ".domain.com");

checking page:

if (isset($_COOKIE['ref']) && $_COOKIE['ref'] == "http://www.yourdomain.com/yourreferingpage.php") {
 //referer is set and is valid
}
0
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
LVL 10

Author Comment

by:wildzero
ID: 11866433
Still dosn't quite solve it, as some of the pages it redirects to I can't alter the page. Simple testing shows that header:location removed the referer.....

I thought there could have been a header:referer that could be set..... or somthing.
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 11866444
I just done a test here

a.php

<?php
header("location: b.php");
?>

b.php

<?php
echo $_SERVER['HTTP_REFERER'];
?>

and b.php produces http://localhost/ - the referer so the header does not remove it
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 11866469
heres my last idea for this one, and is probably even less practical then the previous suggestions

include the file on the second server in a file on the first server with restrictions, example:

server a file (called inc.php for example):

include('http://serverB.com/page.php');

top of page.php on server b

if ($_SERVER['HTTP_HOST'] != "http://serverA.com" || $_SERVER['PHP_SELF'] != "/inc.php") {
 die ("you cannot access this file directly");
}
0
 
LVL 2

Expert Comment

by:nsstone
ID: 11899449
Can you pass the referrer along in another variable (like a hidden input variable) and use it even if it isn't any longer strictly $_REFERRER?
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question