Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.