How to Block IP Addresses with PHP
Figuratively speaking, hunting down and killing spammers, scrapers, and other online scum remains one of our favorite pursuits. Once we have determined that a particular IP address is worthy of banishment, we generally invoke the magical powers of htaccess to lock the gates. When htaccess is not available, we may summon the versatile functionality of PHP to get the job done.
This method is straightforward. Simply edit, copy and paste the following code example into the top of any PHP for which you wish to block access:
<?php $deny = array("111.111.111", "222.222.222", "333.333.333");
if (in_array ($_SERVER['REMOTE_ADDR'], $deny)) {
header("location: https://example.com/");
exit();
} ?>
The code basically creates an array of the IP addresses that you wish to block, and then checks incoming addresses against the array. If the incoming (i.e., remote) address matches against any value in the array, the function will deny access with a redirect header to the specified URL, which in this case is the majestic Google home page. It all happens quickly behind the scenes.
Usage
When using this code in your pages, simply replace the “dummy” IP addresses (i.e., "111.111.111", "222.222.222", ...
) with those that you wish to block (e.g., "123.456.789", "123.456.*", "123.*", ...
). Yes, PHP understands wildcard operators (i.e., *
). Also you may want to change the redirect location. Currently it is set to https://example.com/
, so feel free to change that to whatever URL is desired.
After making any changes, upload the file to your server. If you would like to verify this method, simply lookup your own IP address, add it to the array, and try loading the target page. That’s all there is to it — “grab, gulp, and go”.
Using this method, you may also wish to create a customized page to which blocked addresses are redirected, perhaps to explain the situation, provide contact information, or display a macro shot of your greasy bum, or perhaps send them to the blackhole.
109 responses to “How to Block IP Addresses with PHP”
Hi Jen, I understand your frustration; as far as I know, blogspot doesn’t provide access to the “under-the-hood” mechanics of your site. Unfortunately, that leaves you with only the tools they provide, which may prove inefficient for long-term blog operation. For example, you might be able to require registration before anyone can visit your site. If so, there goes 90% of your traffic. Also, it is easy for anyone (stalker or not) to sign up for a fake account and then you’re back where you started, only with much less traffic..
As for switching to a site that provides access to PHP, htaccess, and other powerful tools, I don’t think there any free hosted sites that offer such luxury. There is Blogger, WordPress, Facebook, MySpace, and all of those places, but I am pretty sure that you would be in the same boat as with Blogspot.
Unfortunately, having control over the “behind-the-scenes” functionality of a website generally requires a self-hosted solution: i.e., domain name, web host, installing your own blog platform — the whole bit. This either requires a lot of time learning the ropes or a lot of money to have someone do it for you.
I hope this was useful for you — Good luck! :)
Thank you for filling me in :)
I’m going to close down blogger…sigh…but I found my blogstalkers through statscounter so I can keep a better eye out at a different site. Unfortunately it is quite an undertaking to notify everyone but feel this is the only way. I am lucky in the fact its not my business site which is on its own domain, I guess I will have to do the same with a blog if I want to keep that hobby :)
Thanks again for trying :D
No problem, Jen — I wish there was more I could do.. Let me know if you get setup with your own blog; I have all sorts of lovely tricks up my sleeve for stopping stalkers, spammers, and other scumbags ;)
Regards,
Jeff
LOL
I know you do! I’ve checked out this blog and don’t worry, I’ll be back :)
My blog is presently closed until Friday where my last post will be up confronting my blogstalker and then the blog will be deleted on tues aug 26. It should be interesting, if you wanted to swing by and see a live blog soap opera this weekend then please do so ;)
I would love to watch the action! Do I need an invite to get in?
lol
no invite…it’ll be posted on friday morning and then I’ll set my blog to ‘everyone’. It was set for everyone until my blogstalker stepped over ‘my line’ of patience yesterday…lol…shut it down to think on things. So on friday it’ll be open.
Oh, I am SO there on Friday ;)
http://humbleopinion2.blogspot.com/2008/08/sigh.html
my last post
the soap opera, episode 1 has begun…bwahahaha (thats my evil laugh…I know, it needs work…lol)
for those that use blog spot this could help I had to put it in my wife’s blog do to someone making nasty comments.
http://toolator.com
For the record, I tried toolator and it only worked for a few days. When the blogstalker’s IP changed, I couldn’t get back in to change the IP at toolator. Nor could add any other webpage or anything…so if this happens to her and you find a way around it, let me know, I’ll add it to my new blog
thanks
Jen, you are hilarious! How did everything go with that last post? I am just getting back from a nice vacation and have been away from the computer and out of the loop..
nice…vacation…lol
it was fine, she realized I could track her (to the point my hubby could leave messages on her comp…lol)and knew who she was so she got other people to track my blog for her…then I closed it down and started new…so far so good…just don’t call me jen over at this new one and she’ll never find me…blogland is a small world…lol