Zixtyy Posted October 24, 2012 Share Posted October 24, 2012 (edited) Hey I'm running a website where I get quite a few proxy users evading bans. As an attempt to block them, I added this code: if(@fsockopen($_SERVER['REMOTE_ADDR'], 80, $errstr, $errno, 1)) die("Your magical proxy adventure ends here."); if(@fsockopen($_SERVER['REMOTE_ADDR'], 3128, $errstr, $errno, 1)) die("Your magical proxy adventure ends here."); if(@fsockopen($_SERVER['REMOTE_ADDR'], 8080, $errstr, $errno, 1)) die("Your magical proxy adventure ends here."); I know it probably could be a little shorter, but it worked which was the main thing. However, a few of my users run legitimate websites off their home networks, so they ended up being blocked by this. I've attempted something, but I am receiving the error: syntax error, unexpected '{', expecting '(' on the first ELSE IF clause. The full code is: if (in_array($_SERVER['REMOTE_ADDR'], $whitelist)) { // User is whitelisted, so we continue without going to the check. } else if { if (@fsockopen($_SERVER['REMOTE_ADDR'], 80, $errstr, $errno, 1) || (@fsockopen($_SERVER['REMOTE_ADDR'], 8080, $errstr, $errno, 1) || (@fsockopen($_SERVER['REMOTE_ADDR'], 3128, $errstr, $errno, 1) } else { // User isn't whitelisted, but is not a proxy, continue! } Can anyone point me in the right direction? Edited October 24, 2012 by Zixtyy Quote Link to comment https://forums.phpfreaks.com/topic/269862-whitelist-for-fsockopen-proxy-check/ Share on other sites More sharing options...
kicken Posted October 24, 2012 Share Posted October 24, 2012 You're missing your condition for your else if() branch. The if below it has extra ( and no ){ at the end. If you just re-structure it a little bit the code becomes easier to read and follow too. if (!in_array($_SERVER['REMOTE_ADDR'], $whitelist)) { if (@fsockopen($_SERVER['REMOTE_ADDR'], 80, $errstr, $errno, 1) || @fsockopen($_SERVER['REMOTE_ADDR'], 8080, $errstr, $errno, 1) || @fsockopen($_SERVER['REMOTE_ADDR'], 3128, $errstr, $errno, 1)){ echo "Banned"; } } Quote Link to comment https://forums.phpfreaks.com/topic/269862-whitelist-for-fsockopen-proxy-check/#findComment-1387482 Share on other sites More sharing options...
Zixtyy Posted October 24, 2012 Author Share Posted October 24, 2012 Nice one! That's a lot neater than my code too. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/269862-whitelist-for-fsockopen-proxy-check/#findComment-1387489 Share on other sites More sharing options...
Christian F. Posted October 24, 2012 Share Posted October 24, 2012 Another way it could be solved, is by having the ports in an array and looping through it. Will make it a bit more extensible, in case you want to add more ports to it later on. Example: $proxyPorts = array (80, 3128, 8080); if (!in_array ($_SERVER['REMOTE_ADDR'], $whitelist)) { foreach ($proxyPorts as $port) { if (@fsockopen ($_SERVER['REMOTE_ADDR'], $port, $errstr, $errno, 1)) { echo 'Banned'; break; } } } Quote Link to comment https://forums.phpfreaks.com/topic/269862-whitelist-for-fsockopen-proxy-check/#findComment-1387544 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.