Jump to content

Redirect script does not redirect properly


replay_

Recommended Posts

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");

?>

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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/");  

?> 

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

<?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?

Link to comment
Share on other sites

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.