Page 1 of 1
IP banning but not from within certain range
Posted: Mon Aug 14, 2006 11:31 am
by Dave2000
I would like to restrict a page from being veiwed, except by certain IPs. For example, mine. My IP isn't static, it's within a small range. Assume, for the purposes of this post, the range is 160.0.0.0 - 160.255.255.255
Is it possible to only allow an IP from this range to view the page.
I have come up with the below so far, but i would like it more specific, if that's possible
Code: Select all
if ( ereg("^160\.", $ip) ) {
Echo 'Error: Invalid IP.</p>';
}
Thank you for any help!
Shears

Posted: Mon Aug 14, 2006 11:46 am
by feyd
In the IPv4 set, your regex covers it. .. although I would strongly suggest converting it to a
preg_match() instead of
ereg().
Posted: Mon Aug 14, 2006 12:06 pm
by Dave2000
Thank you for your reply feyd
In the IPv4 set, your regex covers it
What do you mean by this?
What i would essentually to do, is add a bit more precision to the check - so the IP has to be between 160.0 and 160.255
Shears

Posted: Mon Aug 14, 2006 12:09 pm
by feyd
Your code looks for a start of 160. .. you want to allow a full class A range. Looking for the 160. as the first thing covers a class A range.
Posted: Mon Aug 14, 2006 12:10 pm
by jayshields
Shears wrote:Thank you for your reply feyd
In the IPv4 set, your regex covers it
What do you mean by this?
What i would essentually to do, is add a bit more precision to the check - so the IP has to be between 160.0 and 160.255
Shears

Why would you need to make sure the IP is between 160.0 and 160.255?
feyd is just mentioning your regex would be OK for
ipv4 but for
other ip sets it might not be.
Posted: Mon Aug 14, 2006 12:24 pm
by Dave2000
Thank you for your replies.
Why would you need to make sure the IP is between 160.0 and 160.255?
I could just make sure the IP starts with a "1", but making sure it starts with 160 is more secure/specific. In the same way, making sure the IP is between 160.0 and 160.255 is further secure.
Posted: Mon Aug 14, 2006 12:28 pm
by feyd
Last time I try to explain this:
160. is the same as 160.0 - 160.255. 0-255 is the full possible range of values. Making sure the trailing dot is after the 160 is all that's required to check if the IP, being IPv4, is in the range of 160.0 through 160.255 and all possible children.
Posted: Mon Aug 14, 2006 12:33 pm
by Dave2000
feyd wrote:Last time I try to explain this:
160. is the same as 160.0 - 160.255. 0-255 is the full possible range of values. Making sure the trailing dot is after the 160 is all that's required to check if the IP, being IPv4, is in the range of 160.0 through 160.255 and all possible children.
Thank you. It makes sense now. I did not know 0-255 is the full possible range of values. Sorry to have wasted your time.