Re: Comparing values question.
| From: | Ernest E Vogelsinger | Date: | Sun, 25 May 2003 14:01:45 +0000 |
| Subject: | Re: Comparing values question. | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-148912@lists.php.net to get a copy of this message | ||
At 23:15 24.05.2003, Tom Ray [Lists] said:
--------------------[snip]--------------------
>I've written a guestbook script and I have IP banning in it. Basically
>it checks a flat file with a list of IP address in it and if they match
>the users IP they are denied the ability to post. That works all fine
>and dandy. But here's my question.
>
>How do I get it to compare against a block of IPs? So if I was to add
>1.1.1.* to the list to block everyone from that IP Block, how do I
>compare the 1.1.1.* to like 1.1.1.20 and still have it block? I looked
>over things at PHP.net and I'm guessing I'm just looking over something
>simple. Anyone have any thoughts or suggestions?
--------------------[snip]--------------------
An IP address (dotted notation) is simply a more human-readable notation
for a long value. PHP has the ip2long() and long2ip() functions to help
with that.
Assuming you want to ban the IP range 1.2.3.0 - 1.2.3.255, you could
$ban_base = ip2long('1.2.3.0');
$ban_top = ip2long('1.2.3.255');
All you need to do now is to check if a particular IP is within these borders:
$ip2check = ip2long($_SERVER['CLIENT_ADDRESS']);
if ($ip2check >= $ban_base && $ip2check <= $ban_top) {
// banned IP
}
else {
// IP is ok to post
}
Be aware though that you cannot assume a certain person behind a single IP,
because:
- there might be more than one PCs having this IP (being a proxy server)
- a single PC may change IPs dynamically (Dial-Up, or AOL style)
As to my knowledge, IP-based banning is no good thing to do. You may miss
the target, or you're banning a whole bunch of people you don't want to ban.
Just my 2c worth,
--
>O Ernest E. Vogelsinger
(\) ICQ #13394035
^ http://www.vogelsinger.at/