0ps Posted December 14, 2009 Share Posted December 14, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/185135-need-basic-help-with-refferer/ Share on other sites More sharing options...
mikesta707 Posted December 14, 2009 Share Posted December 14, 2009 $location = (isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : "predefined page"; then use $location in your header. something like that should do Quote Link to comment https://forums.phpfreaks.com/topic/185135-need-basic-help-with-refferer/#findComment-977276 Share on other sites More sharing options...
0ps Posted December 14, 2009 Author Share Posted December 14, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/185135-need-basic-help-with-refferer/#findComment-977283 Share on other sites More sharing options...
mikesta707 Posted December 14, 2009 Share Posted December 14, 2009 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); Quote Link to comment https://forums.phpfreaks.com/topic/185135-need-basic-help-with-refferer/#findComment-977286 Share on other sites More sharing options...
0ps Posted December 14, 2009 Author Share Posted December 14, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/185135-need-basic-help-with-refferer/#findComment-977288 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.