Page 1 of 1
IP Range Help
Posted: Fri Feb 20, 2009 8:46 pm
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");
}
?>
Re: IP Range Help
Posted: Fri Feb 20, 2009 10:55 pm
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.
Re: IP Range Help
Posted: Fri Feb 20, 2009 10:56 pm
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.
Re: IP Range Help
Posted: Sat Feb 21, 2009 10:35 am
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?
Re: IP Range Help
Posted: Sat Feb 21, 2009 4:08 pm
by requinix
So where do you use $_SERVER["REMOTE_ADDR"]?
Re: IP Range Help
Posted: Sat Feb 21, 2009 6:38 pm
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.