haydenm92 Posted August 7, 2009 Share Posted August 7, 2009 I'm new to PHP and I need help with a short script to put on a webpage. Basically I want it to redirect the visitor someplace else if the referrer is blank, and if the referrer is anything else, then just leave them on the page. There are a bunch of smart people around here, I'm sure someone will be able to help me. Any help is greatly appreciated, thanks in advance! Hayden Quote Link to comment Share on other sites More sharing options...
vbnullchar Posted August 7, 2009 Share Posted August 7, 2009 if($_SERVER['PHP_REFERER'] != '') header("location: somewhere.php"); Quote Link to comment Share on other sites More sharing options...
Adam Posted August 7, 2009 Share Posted August 7, 2009 You mean HTTP_REFERER? Quote Link to comment Share on other sites More sharing options...
haydenm92 Posted August 7, 2009 Author Share Posted August 7, 2009 So would i just put <?php if($_SERVER['HTTP_REFERER'] != '') header("location: somewhere.php"); ?> On www.mypage.com Then if I just type www.mypage.com in the browser I will be redirected to somewhere.php? Is this all correct? Thx guys! Quote Link to comment Share on other sites More sharing options...
Adam Posted August 7, 2009 Share Posted August 7, 2009 Should be, obviously unless you're already on a page. Quote Link to comment Share on other sites More sharing options...
haydenm92 Posted August 7, 2009 Author Share Posted August 7, 2009 Alright, so I'm still a bit confused. When I add that piece of code to my html page, it won't redirect when I type the page in my URL. Quote Link to comment Share on other sites More sharing options...
waterssaz Posted August 7, 2009 Share Posted August 7, 2009 $_SERVER['HTTP_REFERER'] cannot always be trusted but if you pop that code at the top of the page (adding the curly braces for the is statment and then enclose the actual page code in an else statemnt I.e <?php if($_SERVER['HTTP_REFERER'] != '') { header("location: somewhere.php"); exit; } else{ // page code to display if referrer does exist } ?> Quote Link to comment Share on other sites More sharing options...
Adam Posted August 7, 2009 Share Posted August 7, 2009 After re-reading your post your logic is backwards, needs to be testing if it's empty, not if it's not empty. Try this: if (!isset($_SERVER['HTTP_REFERER'])) { header("Location: someplace.php"); } Quote Link to comment Share on other sites More sharing options...
haydenm92 Posted August 7, 2009 Author Share Posted August 7, 2009 Ok, so I just figured out I kinda need the exact opposite.. sorry!! What I need my page to do is look for a specific referring URL. If it see's that referring URL, then it needs to redirect to another page. Otherwise, any other traffic that visits that page can stay there. Sorry I was thinking wrong in my previous posts. So does anyone know how to do this? Quote Link to comment Share on other sites More sharing options...
Adam Posted August 7, 2009 Share Posted August 7, 2009 if ($_SERVER['HTTP_REFERER'] == 'url_here') { header("Location: place_to_redirect.php"); } As mentioned though, you can't truely depend on HTTP_REFERER to be accurate. If this is something security related I'd perhaps look into another method. Quote Link to comment Share on other sites More sharing options...
dreamwest Posted August 7, 2009 Share Posted August 7, 2009 Also make sure the referer isnt coming from within your site else youll get loops $referer = $_SERVER['HTTP_REFERER']; if($referer != "yoursite.com" || $referer == "" ) { header("Location: place_to_redirect.php"); } Quote Link to comment Share on other sites More sharing options...
Adam Posted August 7, 2009 Share Posted August 7, 2009 Is this a specific URL or just a domain? If it's a domain use: if (stripos($_SERVER['HTTP_REFERER'], 'domain.com') !== false) { header("Location: ...."); } Quote Link to comment Share on other sites More sharing options...
jonsjava Posted August 7, 2009 Share Posted August 7, 2009 Um...I'd recommend against REFERER checking. Firefox has a great plugin, that I use to read a newspaper for free. it's called RefControl. it lets you tell a server that you are coming from wherever you want it to tell the server. Quote Link to comment 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.