NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page PHP IP Ban/Redirect. Can you help with my script?

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 02-24-2008, 10:32 AM THREAD STARTER               #1 (permalink)
Account Closed
 
X_X_ROB_X_X's Avatar
Join Date: Jun 2007
Location: USA
Posts: 26
X_X_ROB_X_X is an unknown quantity at this point
 



PHP IP Ban/Redirect. Can you help with my script?


Im in need of a little help with my php script if anyone has a bit of time. Simply put what i'm attempting to do is have the php script look at every person's IP as they come to my page. If their IP is in the banned_ip list i'm wanting to redirect that person to the first URL ( http://www.yahoo.com ).
If their IP is not in the list I want to redirect these people to the second URL in the script ( http://www.google.com ).

It's basically a php IP ban/redirect script. I have tried it on my host the way it is written and added my own IP to the banned list and it kept sending me to the 2nd URL when it should have sent me to the 1st. I'm lost. I'm a complete PHP newb guys so please keep the laughing and rude comments to a minimum, lol.

Anyways i'd appreciate any and all help I can get. Below is my code.

????: NamePros.com http://www.namepros.com/programming/436572-php-ip-ban-redirect-can-you.html

Code:
<?php 
$banned_ip = array(); 
$banned_ip[] = '204.96.148.3'; 
$banned_ip[] = '205.162.217.141'; 
$banned_ip[] = '209.62.82.14'; 
$banned_ip[] = '24.7.101.103'; 
$banned_ip[] = '65.31.130.2'; 
$banned_ip[] = '68.168.78.226'; 
$banned_ip[] = '68.205.75.110'; 
$banned_ip[] = '69.36.158.35'; 
$banned_ip[] = '70.173.198.69'; 
$banned_ip[] = '70.97.122.18'; 
$banned_ip[] = '71.189.157.53'; 
$banned_ip[] = '71.230.54.244'; 
$banned_ip[] = '74.52.245.146'; 
$banned_ip[] = '76.90.3.205'; 
$banned_ip[] = '24.20.251.76'; 
$banned_ip[] = '68.83.254.243'; 
$banned_ip[] = '69.2.27.10'; 

foreach($banned_ip as $banned) {  
    $ip = $_SERVER['REMOTE_ADDR']; 
    if($ip == $banned){  
        header("location: http://www.yahoo.com"); 
}
else
{
        header("location: http://www.google.com"); 
        exit();  
    }  
}  


?>
X_X_ROB_X_X is offline  
Old 02-24-2008, 10:51 AM   #2 (permalink)
NamePros Expert
 
Peter's Avatar
Join Date: Nov 2003
Location: Scotland
Posts: 5,069
Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute
 


Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
PHP Code:
<?php 
$banned_ip 
= array();
$banned_ip[] = '204.96.148.3';
????: NamePros.com http://www.namepros.com/showthread.php?t=436572
$banned_ip[] = '205.162.217.141';
$banned_ip[] = '209.62.82.14';
$banned_ip[] = '24.7.101.103';
$banned_ip[] = '65.31.130.2';
$banned_ip[] = '68.168.78.226';
$banned_ip[] = '68.205.75.110';
$banned_ip[] = '69.36.158.35';
$banned_ip[] = '70.173.198.69';
$banned_ip[] = '70.97.122.18';
$banned_ip[] = '71.189.157.53';
$banned_ip[] = '71.230.54.244';
$banned_ip[] = '74.52.245.146';
$banned_ip[] = '76.90.3.205';
????: NamePros.com http://www.namepros.com/showthread.php?t=436572
$banned_ip[] = '24.20.251.76';
$banned_ip[] = '68.83.254.243';
$banned_ip[] = '69.2.27.10';


if(
in_array($_SERVER['REMOTE_ADDR'], $banned_ip)){
    
header("location: http://www.yahoo.com");
}
else
{
    
header("location: http://www.google.com");
    exit();
}
?>

That should work
__________________
Manage your portfolio using my new Domain Portfolio Management script.
Securing Your Domain Name From Theft
Peter is offline  
Old 02-24-2008, 11:14 AM THREAD STARTER               #3 (permalink)
Account Closed
 
X_X_ROB_X_X's Avatar
Join Date: Jun 2007
Location: USA
Posts: 26
X_X_ROB_X_X is an unknown quantity at this point
 



Thank you very much.
Any ideas about banning a range of IPs if I want to use th same script as well. Like if i wanted to ban 208.80.193.[1 - 255]
X_X_ROB_X_X is offline  
Old 02-24-2008, 08:57 PM   #4 (permalink)
i love automation
 
xrvel's Avatar
Join Date: Nov 2007
Location: xrvel.com
Posts: 1,620
xrvel has a brilliant futurexrvel has a brilliant futurexrvel has a brilliant futurexrvel has a brilliant futurexrvel has a brilliant futurexrvel has a brilliant futurexrvel has a brilliant futurexrvel has a brilliant futurexrvel has a brilliant futurexrvel has a brilliant futurexrvel has a brilliant future
 




Originally Posted by X_X_ROB_X_X
Thank you very much.
Any ideas about banning a range of IPs if I want to use th same script as well. Like if i wanted to ban 208.80.193.[1 - 255]
Try this, however, it does not support more than 1 pair of brackets such as [1-3].4.5.[6-7].

PHP Code:
<?php 
$banned_ip 
= array();
$banned_ip[] = '204.96.148.3';
$banned_ip[] = '205.162.217.141';
$banned_ip[] = '209.62.82.14';
$banned_ip[] = '65.31.130.[5-29]';
$banned_ip[] = '68.168.78.226';
$banned_ip[] = '[2-3].1.1.1';
????: NamePros.com http://www.namepros.com/showthread.php?t=436572

$banned_ip parse_range($banned_ip);
echo(
'<pre>'.print_r($banned_iptrue).'</pre>');


if(
in_array($_SERVER['REMOTE_ADDR'], $banned_ip)){
    
//header("location: http://www.yahoo.com");
}
else
{
   
// header("location: http://www.google.com");
    
exit();
}

function 
parse_range($array) {
    
$new_array = array();
    foreach (
$array as $el) {
        if (
preg_match('/\[([0-9]+)\-([0-9]+)\]/'$el$match)) {
            
$from $match[1];
            
$to $match[2];
            
$el preg_replace('/\[([0-9]+)\-([0-9]+)\]/''{REPL}'$el);
            for (
$i $from $i<=$to;$i++) {
                
$new str_replace('{REPL}'$i$el);
                
$new_array[] = $new;
            }
        } else {
            
$new_array[] = $el;
????: NamePros.com http://www.namepros.com/showthread.php?t=436572
        }
    }
    
sort($new_array);
    return(
$new_array);
}
?>
__________________
xrvel is offline  
Old 02-26-2008, 02:26 PM   #5 (permalink)
NamePros Regular
 
monaco's Avatar
Join Date: Jul 2005
Location: Tucson, AZ
Posts: 689
monaco will become famous soon enough
 



If you're going to ban a bunch of IPs, I'd suggest keeping the ban list in a database table somewhere...that for loop is going to start soaking up compute cycles if it gets huge.
__________________
My Website | My Blog
monaco is offline  
Old 02-26-2008, 03:26 PM   #6 (permalink)
Senior Member
 
RegisterRants's Avatar
Join Date: Oct 2006
Location: NJ
Posts: 1,152
RegisterRants has a spectacular aura aboutRegisterRants has a spectacular aura aboutRegisterRants has a spectacular aura about
 




In lieu of the for look, I would use an in_array() check.

Jason
__________________
Web Development
RegisterRants is offline  
Old 02-26-2008, 03:41 PM   #7 (permalink)
NamePros Regular
 
monaco's Avatar
Join Date: Jul 2005
Location: Tucson, AZ
Posts: 689
monaco will become famous soon enough
 



Originally Posted by RegisterRants
In lieu of the for look, I would use an in_array() check.

Jason
Would that not still perform a O(n) sequential search across the whole array?
__________________
My Website | My Blog
monaco is offline  
Old 02-26-2008, 03:43 PM   #8 (permalink)
NamePros Expert
 
Peter's Avatar
Join Date: Nov 2003
Location: Scotland
Posts: 5,069
Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute
 


Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
yes it would. However as it is a built in function written in c it would perform faster than initially doing a for loop yourself. Although it will double the work if you then go ahead and carry out a for loop.
__________________
Manage your portfolio using my new Domain Portfolio Management script.
Securing Your Domain Name From Theft
Peter is offline  
Old 02-26-2008, 04:58 PM   #9 (permalink)
NamePros Regular
 
Palyriot's Avatar
Join Date: Jul 2004
Location: Seattle, Wa
Posts: 596
Palyriot is a jewel in the roughPalyriot is a jewel in the roughPalyriot is a jewel in the rough
 



Maybe make a script that for banning a range of IPs, you type in the range you want to ban and it will add all of those IPs to a database where you can then perform a search query for that IP.
Palyriot is offline  
Old 02-26-2008, 05:49 PM   #10 (permalink)
NamePros Regular
 
monaco's Avatar
Join Date: Jul 2005
Location: Tucson, AZ
Posts: 689
monaco will become famous soon enough
 



You could always make a table with 8 cols (4 octets x 2) then you could do all of the work in a single sql statement which would process ranges too. Add another column for a custom redirect would be a fun touch. Imagine what some banned ppl would think if they got redirected to the DOJ site, or 2girls1cup, or got rickrolled *evil grin*
__________________
My Website | My Blog
monaco is offline  
Old 02-26-2008, 06:34 PM   #11 (permalink)
Senior Member
 
RegisterRants's Avatar
Join Date: Oct 2006
Location: NJ
Posts: 1,152
RegisterRants has a spectacular aura aboutRegisterRants has a spectacular aura aboutRegisterRants has a spectacular aura about
 




I vote for the rickroll
__________________
Web Development
RegisterRants is offline  
Old 02-26-2008, 06:38 PM   #12 (permalink)
NamePros Regular
 
Palyriot's Avatar
Join Date: Jul 2004
Location: Seattle, Wa
Posts: 596
Palyriot is a jewel in the roughPalyriot is a jewel in the roughPalyriot is a jewel in the rough
 



OK, this script should work for any range in any quartile of the IP. Even [0-255].[0-255].[0-255].[0-255] will work. With every $ip_range value you add, it will only get a very small fraction of a second slower. Running the script with 100 ip ranges took 0.00264000892639 seconds.

Have Fun!

PHP Code:
<?php

function checkban($ip$banned_ip$ip_ranges)
{
    
$ipquartiles explode('.'$ip);
    if (
in_array($ip$banned_ip))
    {
        return 
true;
        
    }
    
    if (
$banned != true)
    {
        foreach(
$ip_ranges as $ip_range)
        {
            
$quartiles explode('.'$ip_range);
            for (
$i 0$i <= 4$i++)
            {
                if (
preg_match('/\[([0-9]+)\-([0-9]+)\]/'$quartiles[$i], $matches))
                {
                    
$start $matches[1];
                    
$end $matches[2];
                    if (
$ipquartiles[$i] < $start || $ipquartiles[$i] > $end)
                    {
                        return 
false;
                    }
                    else
????: NamePros.com http://www.namepros.com/showthread.php?t=436572
                    {
                        
$banned true;
                    }
                }
                else
                {
                    if (
$quartiles[$i] != $ipquartiles[$i])
                    {
                        return 
false;
                    }
                }
            }
        }
    }
    return 
$banned;
}

$banned_ip = array();
$banned_ip[] = '204.96.148.3';
$banned_ip[] = '205.162.217.141';

$ip_ranges = array();
$ip_ranges[] = '[0-255].0.0.1';
????: NamePros.com http://www.namepros.com/showthread.php?t=436572

$ip $_SERVER['REMOTE_ADDR'];

$banned checkban($ip$banned_ip$ip_ranges);

if (
$banned == true)
{
    echo 
'banned!';
    
//header("location: http://www.yahoo.com");
}
else
{
   
// header("location: http://www.google.com");
   
echo 'not banned';
}

?>
Palyriot is offline  
Old 02-28-2008, 11:05 PM   #13 (permalink)
NamePros Regular
 
Palyriot's Avatar
Join Date: Jul 2004
Location: Seattle, Wa
Posts: 596
Palyriot is a jewel in the roughPalyriot is a jewel in the roughPalyriot is a jewel in the rough
 



Any updates? Did you try my script out?
Palyriot is offline  
Old 03-11-2008, 11:48 PM   #14 (permalink)
New Member
Join Date: Mar 2008
Posts: 4
mhuynh is an unknown quantity at this point
 



Derek "Palyriot"...

You seem like a real GURU at PHP... I'm also doing something similar I was going through your code and it works great.... I modified it a bit to make it suitable for what i need it to do... but what I need to do is to block out all IP ranges and exclude a few.

Is it possible to exclude some IPs or IP ranges ?
mhuynh is offline  
Old 03-12-2008, 04:40 PM   #15 (permalink)
New Member
Join Date: Mar 2008
Posts: 4
mhuynh is an unknown quantity at this point
 



Hey guys...

what I did to block out all IP's except these range and this ip ... I'm a bit of a noob as well at this but, if would be nice if someone could just have a quick look to see if it is right... I just need ip range of 150.203.197.*** to work, and 136.153.2.2 ... And reject all other ips.

thanks in advanced...
????: NamePros.com http://www.namepros.com/showthread.php?t=436572

<?php

$valid_ips = array('150.203.197..*','136.153.2.2');
if (!in_array($_SERVER['REMOTE_ADDR'],$valid_ips)) {
echo "Access denied. Please go return to menu screen you must login in with your barcode and password before you can access this page.";

//header('Location: /index.php'); //change according to your site if needed
exit();
}

?>
mhuynh is offline  
Old 03-12-2008, 08:32 PM   #16 (permalink)
NamePros Regular
 
Palyriot's Avatar
Join Date: Jul 2004
Location: Seattle, Wa
Posts: 596
Palyriot is a jewel in the roughPalyriot is a jewel in the roughPalyriot is a jewel in the rough
 



This should do it

PHP Code:
  <?php

function checkban($ip$banned_ip$ip_ranges$valid_ip)
{
    if (
in_array($ip$valid_ip))
    {
        return 
false;
    }
    
$ipquartiles explode('.'$ip);
    if (
in_array($ip$banned_ip))
    {
????: NamePros.com http://www.namepros.com/showthread.php?t=436572
        return 
true;
    }
    foreach(
$ip_ranges as $ip_range)
    {
        
$quartiles explode('.'$ip_range);
        for (
$i 0$i <= 4$i++)
        {
            if (
preg_match('/\[([0-9]+)\-([0-9]+)\]/'$quartiles[$i], $matches))
            {
                
$start $matches[1];
                
$end $matches[2];
                if (
$ipquartiles[$i] < $start || $ipquartiles[$i] > $end)
                {
                    return 
false;
                }
                else
                {
                    
$banned true;
                }
            }
            else
            {
                if (
$quartiles[$i] != $ipquartiles[$i])
                {
                    return 
false;
                }
            }
        }
    }
    return 
$banned;
}

$valid_ip = array();
$valid_ip[] = '127.0.0.1';

$banned_ip = array();
$banned_ip[] = '204.96.148.3';
$banned_ip[] = '205.162.217.141';

$ip_ranges = array();
$ip_ranges[] = '[0-255].0.0.1';

$ip $_SERVER['REMOTE_ADDR'];

$banned checkban($ip$banned_ip$ip_ranges$valid_ip);

if (
$banned == true)
????: NamePros.com http://www.namepros.com/showthread.php?t=436572
{
    echo 
'banned!';
    
//header("location: http://www.yahoo.com");
}
else
{
   
// header("location: http://www.google.com");
   
echo 'not banned';
}

?>
Palyriot is offline  
Old 03-13-2008, 06:17 PM   #17 (permalink)
New Member
Join Date: Mar 2008
Posts: 4
mhuynh is an unknown quantity at this point
 



Thanks dude.... !!! Your the best !!!

I'm going to give it a go....
mhuynh is offline  
Old 03-13-2008, 07:23 PM   #18 (permalink)
NamePros Regular
 
Palyriot's Avatar
Join Date: Jul 2004
Location: Seattle, Wa
Posts: 596
Palyriot is a jewel in the roughPalyriot is a jewel in the roughPalyriot is a jewel in the rough
 



Cool, tell me if there are any problems with it.
Palyriot is offline  
Old 03-13-2008, 09:12 PM   #19 (permalink)
New Member
Join Date: Mar 2008
Posts: 4
mhuynh is an unknown quantity at this point
 



Awesome.... dude...
Alright ... I've added the ip_ranges array

Code:
$ip_ranges[] = '[0-255].[0-255].[0-255].[0-255]';
That blocked out all the IPs [which is what I wanted]...

In valid_ip array I added
Code:
$valid_ip[] = '136.153.2.2';
I'm assuming that I can't add valid IP ranges in there right?

I've tried to play around with the figures in the ip_ranges array to skip this range '150.203.197.*' but I couldn't get it to work....

I must be doing something wrong I think.... I've playing around with you code at all morning to try to rejig it a bit but, I still be unsucessful.

hmm....
mhuynh is offline  
Old 03-14-2008, 03:21 AM   #20 (permalink)
New Member
Join Date: Mar 2008
Posts: 4
boleemenee is an unknown quantity at this point
 



I'm surprised that no one mentioned .htaccess mod_rewrite and mod_access, this apache modules works faster and better, was made for this kind of tasks and enabled everywhere by default. If you need dynamic IP lists - you can simply remove and add IPs to .htaccess file (manually or via php).
__________________
;)
boleemenee is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Liquid Web Smart Servers  
All times are GMT -7. The time now is 03:26 AM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger