Jump to content

Need basic help with refferer


0ps

Recommended Posts

I have a script that detects the referring page and then redirects back to that page when its finished. However if the user is blocking the referrer they get left with a blank page.

 

So I want to redirect to the referrer if one exists and if there isn't a referrer redirect to a predefined page. I'm pretty sure what I want to do is basic but my php knowledge is limited.

 

the code for detecting referrer and redirecting back to it

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

 

next part is where I'm stuck, I need a way to use to the following code if the referrer from the above code is blank

header('Location:index.php');

 

Thanks in advance for any help.

Link to comment
https://forums.phpfreaks.com/topic/185135-need-basic-help-with-refferer/
Share on other sites

Thanks for the fast reply.

 

I think there is a typo in your last post with this part

 :

I really don't know much about php but I tried the following

$location = (isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : "index.php");
header('Location:$location');

 

Which resulted in a page not found error regardless of referrer being present or not.

yeah I don't really know how that happened... Chrome started typing in this weird font for a min.

 

but thats close, single quotes don't interpolate variables, so that header string is the literal string "Location:$location". Notice it uses the literal string $location rather than the value of that variable. You can easily fix this by concatenating, or using double quoes.

 

Here is the former example

header('Location :'.$location);

Most of what you said went over my head but I tried your example

$location = (isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : "index.php");
header('Location :'.$location);

 

Which now results in a blank page regardless of referrer being present. So it would appear its not redirecting at all now.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.