IP banning but not from within certain range

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
Dave2000
Forum Contributor
Posts: 126
Joined: Wed Jun 21, 2006 1:48 pm

IP banning but not from within certain range

Post 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 :)
Last edited by Dave2000 on Mon Aug 14, 2006 11:59 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

In the IPv4 set, your regex covers it. .. although I would strongly suggest converting it to a preg_match() instead of ereg().
Dave2000
Forum Contributor
Posts: 126
Joined: Wed Jun 21, 2006 1:48 pm

Post 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 :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post 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.
Dave2000
Forum Contributor
Posts: 126
Joined: Wed Jun 21, 2006 1:48 pm

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Dave2000
Forum Contributor
Posts: 126
Joined: Wed Jun 21, 2006 1:48 pm

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