replay_ Posted February 8, 2011 Share Posted February 8, 2011 I'm trying to put together a script that redirects visitors based on their IP, user agent and/or referral url. Basically I want the script to scan these three factors from the visitor, if any of them turn out to match my redirect-requirement it redirects the user. I know the code is horribly coded, I'm incredibly new to the php-scene and consider myself a complete noob. As you can see I want redirected visitors to go to google.com and un-redirected to msn.com(examples). Really thankful for all the help I can get! Right now nothing works, any suggestions? <?php function redirect($page) { Header( "HTTP/1.1 301 Moved Permanently" ); header('Location: ' . $page); exit; } $referrals=array('pitchingit.org','referral2'); $badAgents = array("useragent1", "useragent2"); $deny = array("78.105.191..*","100.101.103..*"); if (in_array($_SERVER['HTTP_REFERER'], $referrals, FALSE)) { header("Location: http://www.google.com"); } else { header("Location: http://www.msn.com"); } if(in_array($_SERVER['HTTP_USER_AGENT'],$badAgents)) { redirect("http://www.google.com/"); exit(); } $add=$_SERVER['REMOTE_ADDR']; foreach ($deny as $ip) { if (preg_match("^.$add.*^",$ip)) { redirect("http://www.google.com"); } } redirect("http://www.msn.com"); ?> Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted February 8, 2011 Share Posted February 8, 2011 Do you have error_reporting at -1 or E_ALL, and display_errors set to On? Quote Link to comment Share on other sites More sharing options...
replay_ Posted February 8, 2011 Author Share Posted February 8, 2011 Do you have error_reporting at -1 or E_ALL, and display_errors set to On? I've only put my host to show php-errors, I'm coding in textwrangler atm, what's that you're mentioning there? Completely new here. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted February 8, 2011 Share Posted February 8, 2011 You can also set error_reporting on a per script basis. At the top of the script, right after the opening <?php tag, add this and see if it shows any errors when you execute it. error_reporting(-1); ini_set('display_errors', 1); Quote Link to comment Share on other sites More sharing options...
replay_ Posted February 8, 2011 Author Share Posted February 8, 2011 You can also set error_reporting on a per script basis. At the top of the script, right after the opening <?php tag, add this and see if it shows any errors when you execute it. error_reporting(-1); ini_set('display_errors', 1); I'm not getting any errors, I'm simply not being redirected to google when I enter my own ip or click on the link through my own domain. Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted February 8, 2011 Share Posted February 8, 2011 you should check to see if $_SERVER['HTTP_REFERER'] is set before referencing it as an array: if (isset($_SERVER['HTTP_REFERER']) && in_array($_SERVER['HTTP_REFERER'], $referrals, FALSE)) { and this line is unnecessary and potentially causing problems: Header( "HTTP/1.1 301 Moved Permanently" ); // REMOVE THIS Quote Link to comment Share on other sites More sharing options...
replay_ Posted February 8, 2011 Author Share Posted February 8, 2011 you should check to see if $_SERVER['HTTP_REFERER'] is set before referencing it as an array: if (isset($_SERVER['HTTP_REFERER']) && in_array($_SERVER['HTTP_REFERER'], $referrals, FALSE)) { and this line is unnecessary and potentially causing problems: Header( "HTTP/1.1 301 Moved Permanently" ); // REMOVE THIS Thanks for your input but the script still does not redirect properly, I tried inserting my own IP and ended up at MSN. I found a way to code my IP-redirect but my PHP-skills end there, it's the user agent and referrer redirect I can't seem to get! How would you add those two redirects to this code? <?php function redirect($page) { header('Location: ' . $page); exit; } $deny = array('/my.ip.working..*/','/100.101.103..*/'); foreach ($deny as $ip) { if (preg_match($ip,$_SERVER['REMOTE_ADDR'])) { redirect("http://www.google.com/"); } } redirect("http://www.msn.com/"); ?> Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted February 9, 2011 Share Posted February 9, 2011 <?php /* Standard redirect function (for re-use) */ function redirect($url) { header('Location: ' . $url); } // Setup URL's $url['ip'] = 'http://www.google.com'; $url['referrer'] = 'http://www.yahoo.com'; $url['agent'] = 'http://www.msn.com'; // Setup rules for redirection $referrers = array('yousuck.org', 'welikefruit.com'); $badAgents = array('useragent1', 'useragent2'); $deny = array('78.105.191.38'); /* Check for IP Redirects */ if (!empty($deny)) { // Only if there are denied IP's foreach ($deny as $ip) { if ($ip == $_SERVER['REMOTE_ADDR']) { redirect($url['ip']); } } } /* Check for Referrer Redirects */ if (isset($_SERVER['HTTP_REFERRER'])) { if (!empty($referrers)) { foreach ($referrers as $value) { if (strstr($_SERVER['HTTP_REFERRER'], $value) === true) { redirect($url['referrer']); } } } } /* Check for Agent redirects */ if (isset($_SERVER['HTTP_USER_AGENT'])) { if (!empty($badAgents)) { foreach ($badAgents as $value) { if (strstr($_SERVER['HTTP_USER_AGENT'], $value) === true) { redirect($url['agent']); } } } } // Activate fallback redirect redirect("http://www.msn.com"); ?> 1. This should work. I cannot guarantee that it works entirely because I barely tested it. If this does not work, then you could easily get a working solution from this within a few minutes. 2. Understand, you cannot rely on referrers. They are not always trust worthy. Neither is user agent. Neither is IP for that matter. Good luck. Quote Link to comment Share on other sites More sharing options...
replay_ Posted February 9, 2011 Author Share Posted February 9, 2011 <?php /* Standard redirect function (for re-use) */ function redirect($url) { header('Location: ' . $url); } // Setup URL's $url['ip'] = 'http://www.google.com'; $url['referrer'] = 'http://www.yahoo.com'; $url['agent'] = 'http://www.msn.com'; // Setup rules for redirection $referrers = array('yousuck.org', 'welikefruit.com'); $badAgents = array('useragent1', 'useragent2'); $deny = array('78.105.191.38'); /* Check for IP Redirects */ if (!empty($deny)) { // Only if there are denied IP's foreach ($deny as $ip) { if ($ip == $_SERVER['REMOTE_ADDR']) { redirect($url['ip']); } } } /* Check for Referrer Redirects */ if (isset($_SERVER['HTTP_REFERRER'])) { if (!empty($referrers)) { foreach ($referrers as $value) { if (strstr($_SERVER['HTTP_REFERRER'], $value) === true) { redirect($url['referrer']); } } } } /* Check for Agent redirects */ if (isset($_SERVER['HTTP_USER_AGENT'])) { if (!empty($badAgents)) { foreach ($badAgents as $value) { if (strstr($_SERVER['HTTP_USER_AGENT'], $value) === true) { redirect($url['agent']); } } } } // Activate fallback redirect redirect("http://www.msn.com"); ?> 1. This should work. I cannot guarantee that it works entirely because I barely tested it. If this does not work, then you could easily get a working solution from this within a few minutes. 2. Understand, you cannot rely on referrers. They are not always trust worthy. Neither is user agent. Neither is IP for that matter. Good luck. Thanks for what looks to be a very thorough and well-built script, however I can't seem to get any of the parameters to work($ip,$referral,$useragent)? Also tried switching the deny array for IP's to '/100.101.102..*/','/100.101.103..*/' in order to block ranges but that didn't seem to work either? Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted February 9, 2011 Share Posted February 9, 2011 the $url array handles JUST the url's for redirect. Those just need to be the URL's you want to redirect to. The arrays you need to change for what is blocked are $referrers, $badAgents, and $deny which is all below the url array. 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.