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 Link to comment https://forums.phpfreaks.com/topic/169194-need-help-with-short-script/ 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"); Link to comment https://forums.phpfreaks.com/topic/169194-need-help-with-short-script/#findComment-892733 Share on other sites More sharing options...
Adam Posted August 7, 2009 Share Posted August 7, 2009 You mean HTTP_REFERER? Link to comment https://forums.phpfreaks.com/topic/169194-need-help-with-short-script/#findComment-892735 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! Link to comment https://forums.phpfreaks.com/topic/169194-need-help-with-short-script/#findComment-892740 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. Link to comment https://forums.phpfreaks.com/topic/169194-need-help-with-short-script/#findComment-892741 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. Link to comment https://forums.phpfreaks.com/topic/169194-need-help-with-short-script/#findComment-892750 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 } ?> Link to comment https://forums.phpfreaks.com/topic/169194-need-help-with-short-script/#findComment-892755 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"); } Link to comment https://forums.phpfreaks.com/topic/169194-need-help-with-short-script/#findComment-892756 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? Link to comment https://forums.phpfreaks.com/topic/169194-need-help-with-short-script/#findComment-892766 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. Link to comment https://forums.phpfreaks.com/topic/169194-need-help-with-short-script/#findComment-892770 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"); } Link to comment https://forums.phpfreaks.com/topic/169194-need-help-with-short-script/#findComment-892776 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: ...."); } Link to comment https://forums.phpfreaks.com/topic/169194-need-help-with-short-script/#findComment-892864 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. Link to comment https://forums.phpfreaks.com/topic/169194-need-help-with-short-script/#findComment-892878 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.