Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Blocking proxies with PHP snippet



9:52 am on Apr 16, 2014 (gmt 0)

5+ Year Member

I've got this bit of hacked up code from searches on the internet. Obviously I'm not about to deploy something I Googled on my server without learning exactly what it's doing, so I wonder if someone could talk me through it. My primary concern is to block the majority of tough proxies WITHOUT causing any ranking or indexing issues with search engines...

<?php if(@fsockopen($_SERVER['REMOTE_ADDR'], 80, $errstr, $errno, 1)) die("Proxy access not allowed"); ?>


9:54 am on Apr 16, 2014 (gmt 0)

5+ Year Member

{Double post, sorry}


3:27 pm on Apr 16, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

This isn't my area, but until someone who actually knows what they're talking about responds, you might try some test code on typical search engine bot requests from your server logs to see how their hostnames respond to HTTP requests. And I have a feeling that you want the code to exit more elegantly than with a simple die().


12:57 pm on Apr 18, 2014 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

The logic of that if statement is saying that

if you can open a port 80 connection for the remote address (the IP from which the request initiated) within 1 second then kill the request with the message specified

Basically it's checking to see if an HTTP server is running on the IP address that initiated the request by trying to connect to it.


4:20 pm on Apr 18, 2014 (gmt 0)

5+ Year Member

Thanks. I actually meant to post the following version of the code, which allegedly is not going to upset search engine spiders (whereas that in the first post allegedly will). Why, I wonder?...

<?php if(@fsockopen($_SERVER['REMOTE_ADDR'], 8080, $errstr, $errno, 1) or @fsockopen($_SERVER['REMOTE_ADDR'], 1080, $errstr, $errno, 1) )
die("Proxy access not allowed"); ?>


8:14 pm on Apr 18, 2014 (gmt 0)

WebmasterWorld Administrator 5+ Year Member Top Contributors Of The Month

It is more work to determine where the evil proxies are and block them, but you would avoid blocking innocent visitors that way. If you are going to wholesale block proxies you should offer a way for human visitors to get a pass.

For many years, satellite internet was the only option available to me and every request is a proxy request because all traffic is routed through their service centers in a few locations. It is pretty maddening to visit a site and find yourself blocked with no appeal. I sometimes use a mobile modem, same issue. Not all proxy connections are evil.


1:47 am on Apr 19, 2014 (gmt 0)

5+ Year Member

This is something that is only likely to be in place for a few weeks. I can live with upsetting a few innocent visitors in order to keep the undesirable filth away from my website during that important period. My primary concern is doing it in a way that will not get my site de-indexed.


2:11 am on Apr 19, 2014 (gmt 0)

WebmasterWorld Administrator 5+ Year Member Top Contributors Of The Month

In this case, I have to say I believe I'd do it myself.

Featured Threads

Hot Threads This Week

Hot Threads This Month