[advanced search]
 

Go Back   NamePros.com > Discussion > Web Design & Development > Programming

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


Closed Thread
 
LinkBack Thread Tools
Old 02-24-2008, 09:32 AM   #1 (permalink)
Account Closed
 
X_X_ROB_X_X's Avatar
 
Join Date: Jun 2007
Location: USA
Posts: 26
0.00 NP$ (Donate)

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.


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, 09:51 AM   #2 (permalink)
Senior Member
 
Peter's Avatar
 
Join Date: Nov 2003
Location: Scotland
Posts: 4,900
0.60 NP$ (Donate)

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';
$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';


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, 10:14 AM   #3 (permalink)
Account Closed
 
X_X_ROB_X_X's Avatar
 
Join Date: Jun 2007
Location: USA
Posts: 26
0.00 NP$ (Donate)

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, 07:57 PM   #4 (permalink)
i love automation
 
xrvel's Avatar
 
Join Date: Nov 2007
Posts: 1,409
987.78 NP$ (Donate)

xrvel has much to be proud ofxrvel has much to be proud ofxrvel has much to be proud ofxrvel has much to be proud ofxrvel has much to be proud ofxrvel has much to be proud ofxrvel has much to be proud ofxrvel has much to be proud ofxrvel has much to be proud ofxrvel has much to be proud of


Quote:
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';

$banned_ip = parse_range($banned_ip);
echo(
'<pre>'.print_r($banned_ip, true).'</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;
        }
    }
    
sort($new_array);
    return(
$new_array);
}
?>
xrvel is offline  
Old 02-26-2008, 01:26 PM   #5 (permalink)
NamePros Regular
 
monaco's Avatar
 
Join Date: Jul 2005
Location: Tucson, AZ
Posts: 695
314.80 NP$ (Donate)

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, 02:26 PM   #6 (permalink)
Senior Member
 
RegisterRants's Avatar
 
Join Date: Oct 2006
Posts: 1,144
257.37 NP$ (Donate)

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, 02:41 PM   #7 (permalink)
NamePros Regular
 
monaco's Avatar
 
Join Date: Jul 2005
Location: Tucson, AZ
Posts: 695
314.80 NP$ (Donate)

monaco will become famous soon enough


Quote:
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, 02:43 PM   #8 (permalink)
Senior Member
 
Peter's Avatar
 
Join Date: Nov 2003
Location: Scotland
Posts: 4,900
0.60 NP$ (Donate)

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, 03:58 PM   #9 (permalink)
NamePros Regular
 
Palyriot's Avatar
 
Join Date: Jul 2004
Location: Seattle, Wa
Posts: 596
76.25 NP$ (Donate)

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, 04:49 PM   #10 (permalink)
NamePros Regular
 
monaco's Avatar
 
Join Date: Jul 2005
Location: Tucson, AZ
Posts: 695
314.80 NP$ (Donate)

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, 05:34 PM   #11 (permalink)
Senior Member
 
RegisterRants's Avatar
 
Join Date: Oct 2006
Posts: 1,144
257.37 NP$ (Donate)

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, 05:38 PM   #12 (permalink)
NamePros Regular
 
Palyriot's Avatar
 
Join Date: Jul 2004
Location: Seattle, Wa
Posts: 596
76.25 NP$ (Donate)

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
                    {
                        
$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';

$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, 10:05 PM   #13 (permalink)
NamePros Regular
 
Palyriot's Avatar
 
Join Date: Jul 2004
Location: Seattle, Wa
Posts: 596
76.25 NP$ (Donate)

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, 10:48 PM   #14 (permalink)
New Member
 
Join Date: Mar 2008
Posts: 4
0.00 NP$ (Donate)

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, 03:40 PM   #15 (permalink)
New Member
 
Join Date: Mar 2008
Posts: 4
0.00 NP$ (Donate)

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

<?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, 07:32 PM   #16 (permalink)
NamePros Regular
 
Palyriot's Avatar
 
Join Date: Jul 2004
Location: Seattle, Wa
Posts: 596
76.25 NP$ (Donate)

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))
    {
        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)
{
    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, 05:17 PM   #17 (permalink)
New Member
 
Join Date: Mar 2008
Posts: 4
0.00 NP$ (Donate)

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, 06:23 PM   #18 (permalink)
NamePros Regular
 
Palyriot's Avatar
 
Join Date: Jul 2004
Location: Seattle, Wa
Posts: 596
76.25 NP$ (Donate)

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, 08:12 PM   #19 (permalink)
New Member
 
Join Date: Mar 2008
Posts: 4
0.00 NP$ (Donate)

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, 02:21 AM   #20 (permalink)
New Member
 
Join Date: Mar 2008
Posts: 4
0.00 NP$ (Donate)

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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Site Sponsors
Advertise your business at NamePros

All times are GMT -7. The time now is 07:47 PM.


Powered by: vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0
Template-Modifications by TMS
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85