I'd like, for example, block every IP from base 89.95 (89.95..).
I don't have .htaccess
files on my server, so I'll have to do it with PHP.
if ($_SERVER['REMOTE_ADDR'] == "89.95.25.37") die();
Would block specific IP. How can I block entire IP blocks?
Thank you very much.
using revive's code, use this to get wildcard search working
Make a substring :) For example for blocking 89.95.25.* you make a substring of the IP, cutting off the last two numbers and compare it to "89.95.25."
Use
ip2long()
to convert dotted decimal to a real IP address. Then you can do ranges easily.Just do
ip2long()
on the high and low range to get the value, then use those as constants in your code.If you're familiar with subnet masking, you can do it like this:
Or if you're familiar with this format
10.12.0.0/16
:You can turn these into functions and have very manageable code, making it easy to add IP addresses and customize the ranges.
Convert the dotted quad to an integer:
Try
strpos()
If you notice, the
===
operator makes sure that the89.95
is at the begining of the IP address. This means that you can sepcify as much of the IP address as you want, and it will block no matter what numbers come after it.For instance, all of these will be blocked:
89.95 ->
89.95.12.34
,89.95.1234.1
,89.95.1.1
89.95.6 ->
89.95.65.34
,89.95.61.1
,89.95.6987
(some of those aren't valid IP addresses though)