why is my header("Location: $_SERVER['HTTP_REFERER']"); PHP function not working?
Asked Answered
T

7

7

It works when I input

header("Location: http://www.google.com");

but it doesn't work when I have

header("Location: $_SERVER['HTTP_REFERER']");

I want to redirect the page to whatever page it came from.

Tricycle answered 3/11, 2011 at 19:2 Comment(4)
Not all browsers send a referer, and some that do, send bogus data. As well, you can't use quotes on an array key within a double-quoted string like that. try header("Location: {$_SERVER['HTTP_REFERER']}"); instead.Unrig
Have you checked the contents of $_SERVER['HTTP_REFERER']?Wards
what is the output of this string: echo "Location: $_SERVER['HTTP_REFERER']"; ?Incubus
@MarcB is right. You can verify this behavior by firing up Fiddler (fiddler2.com/fiddler2) and taking a look at the headers you get back. I always concat variables like this rather than putting them inline, so code is easy to read.Mide
C
9

Try it: :)

if (!empty($_SERVER['HTTP_REFERER']))
    header("Location: ".$_SERVER['HTTP_REFERER']);
else
   echo "No referrer.";

However, for determining which page user came from, I'd rather use session variable, which gets reset at every page:

session_start();
echo "Previous page:", $_SESSION['loc'];
$_SESSION['loc']=$_SERVER['PHP_SELF'];

ps: This only works for local pages, you cannot track other websites.

Cima answered 3/11, 2011 at 19:7 Comment(0)
P
4

You might try:

header("Location: {$_SERVER['HTTP_REFERER']}");

I've had problems with variable expressions which contain quotes in strings without braces.

You also need to look out for $_SERVER['HTTP_REFERER'] simply not being set. Some user agents don't set it, some privary tools mask it, and you need to handle people coming to your page without a referrer.

Proportional answered 3/11, 2011 at 19:8 Comment(0)
S
3

Here is a simple solution. check and see what $_server['http_referer'] is giving you and if its set then you can redirect and if not put a fall back url something like :

if(isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER'] != ""){
$url = $_SERVER['HTTP_REFERER'];
}else{
$url = "YOUR INDEX PAGE OR SOMETHING";
}

header("Location: ".$url);
Summer answered 3/11, 2011 at 19:8 Comment(0)
P
2

This is a browser feature, and any polite browser will send the correct header (although various 'security' tools will override this with a fake referer).

It's browser specific so not every browser/security software combination will send it to the server. You're better off setting a session variable on each page load to determine which page the user came from (or something similar with a bit more logic)

Polemics answered 3/11, 2011 at 19:8 Comment(1)
so make a session of the current url on each page?Tricycle
C
1
header("Location: $_SERVER[HTTP_REFERER]");

Without the single quotes. This is the fastest way to access and concatenate array values without extra concatenating code.

Centralize answered 11/6, 2015 at 15:4 Comment(0)
N
0

Simply you can use

if(isset($_SERVER['HTTP_REFERER'])){
    header("Location:".$_SERVER['HTTP_REFERER']."");
}
Nocturne answered 14/11, 2014 at 6:10 Comment(0)
L
0

One of the mistakes that occure sometimes is, that NO OUTPUT must happen before header('Location: ' ....)

This is not working (shows the output, but doesn't redirect):

if (isset($_SERVER['HTTP_REFERER'])) {
    $referer = $_SERVER['HTTP_REFERER'];
    $cleaned_url = preg_replace('/[^a-z ]+/i', '', strtolower($referer));
    $pattern = '/troester/';
    $res = preg_match($pattern, $cleaned_url);
    echo $res; // <--- OUTPUT COMES HERE
    if ($res == true) header("Location: {$referer}");
}

This is working (does redirect properly):

if (isset($_SERVER['HTTP_REFERER'])) {
    $referer = $_SERVER['HTTP_REFERER'];
    $cleaned_url = preg_replace('/[^a-z ]+/i', '', strtolower($referer));
    $pattern = '/troester/';
    $res = preg_match($pattern, $cleaned_url);
    //echo $res; // <--- NO OUTPUT COMES HERE
    if ($res == true) header("Location: {$referer}");
}

This is also working, but doesn't make sense ():

if (isset($_SERVER['HTTP_REFERER'])) {
    $referer = $_SERVER['HTTP_REFERER'];
    $cleaned_url = preg_replace('/[^a-z ]+/i', '', strtolower($referer));
    $pattern = '/troester/';
    $res = preg_match($pattern, $cleaned_url);
    if ($res == true) header("Location: {$referer}");
    echo $res; // <--- OUTPUT COMES HERE, AFTER header('Location: ' ....)
}

(For better understandig, hope this may help)

Lovable answered 28/11, 2018 at 12:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.