IP Range Help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
HSKrustofsky
Forum Newbie
Posts: 14
Joined: Sun Oct 26, 2008 11:45 pm

IP Range Help

Post by HSKrustofsky »

I am trying to allow multiple IP ranges. The script below works up until I want to add another IP range. Instead of only allowing the IP range, it allows all. I've tried changing $ip to $ip2, and $check to $check2, but nothing. Any suggestions?

Code: Select all

 
<?php
 
$ip = "123.123.45";
 
$check  = explode('.', $ip);
 
if( $check[2] >=15 && $check[2] <=45){
    header("Location:http://".$_SERVER['HTTP_HOST']."/enter.html"); 
}
 
?>
 
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: IP Range Help

Post by JAB Creations »

Technically you're supposed to use Apache (or whatever web server software you're using) to accomplish this. I don't whitelist IP addresses in Apache which is what it looks like what you're trying. I'd recommend posting in the Miscellaneous about Apache IP white list range...an accurate description of what I think you're attempting to do here with PHP.
semlar
Forum Commoner
Posts: 61
Joined: Fri Feb 20, 2009 10:45 pm

Re: IP Range Help

Post by semlar »

Well, you're just checking the third set of numbers to be between 15 and 45, you're not even comparing the other two sets.

You probably want something more like

Code: Select all

<?php
$ip = "123.123.45";
$check  = explode('.', $ip);
if( $check[0] == 123 && $check[1] == 123 && $check[2] >=15 && $check[2] <=45){
    header("Location:http://".$_SERVER['HTTP_HOST']."/enter.html");
}
?>
Granted, that isn't exactly a secure method for whitelisting IP addresses, if that's what you're doing.
HSKrustofsky
Forum Newbie
Posts: 14
Joined: Sun Oct 26, 2008 11:45 pm

Re: IP Range Help

Post by HSKrustofsky »

For some weird reason its only works with one. I am trying to add multiple IPs, and it won't let me. Here is the code I am trying to work with...

Code: Select all

 
                $ip = "123.123.75";
        
        $check  = explode('.', $ip);
 
        if( $check[0] == 123 && $check[1] == 123 && $check[2] >= 34 && $check[2] <= 75)
            {
            header("Location:http://".$_SERVER['HTTP_HOST']."/enter.html");
            }
        
        $ip2 = "123.123.123.190";
        
        $check2  = explode('.', $ip2);
 
        if( $check2[0] == 123 && $check2[1] == 123 && $check2[2] == 123 && $check2[3] >= 1 && $check2[3] <=190)
            {
            header("Location:http://".$_SERVER['HTTP_HOST']."/enter.html");
            }
 
Well what is happening is, instead of only allowing these IP ranges to enter.html, it is allowing everyone, and I don't want that. Any suggestions?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: IP Range Help

Post by requinix »

So where do you use $_SERVER["REMOTE_ADDR"]?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: IP Range Help

Post by califdon »

If that's really your code, all you are doing is checking to see that a number is what you already defined it to be, so sure, it will have no bearing on what IP address is requesting the page.

JAB Creations had it right, you shouldn't be trying to do this in your application code, it's part of the Apache security system.
Post Reply