...

View Full Version : An easier way to write this filter?



ArcticFox
12-24-2007, 05:46 AM
I'm wondering if there's an easier way to write this. The way I have it now, I can see what each block is for in the //commented section, but the code is obviously bloated.


if (strstr($_SERVER["REMOTE_ADDR"], "66.231.188.156")) {exit;} //Gigabot
if (strstr($_SERVER["REMOTE_ADDR"], "208.99.195.54")) {exit;} //acts like a bot
if (strstr($_SERVER["REMOTE_ADDR"], "38.100.41.101")) {exit;} //acts like a bot
if (strstr($_SERVER["REMOTE_ADDR"], "69.41.13.5")) {exit;} //Hidden information



if (strstr($_SERVER["HTTP_USER_AGENT"], "Java")) {exit;} //bot
if (strstr($_SERVER["HTTP_USER_AGENT"], "XX")) {exit;} //hidden?
if (strstr($_SERVER["HTTP_USER_AGENT"], "Windows 95")) {exit;} //what?
if (strstr($_SERVER["HTTP_USER_AGENT"], "Windows 98")) {exit;} //when?
if (strstr($_SERVER["HTTP_USER_AGENT"], "undefined")) {exit;} //who dis?



if (strstr($_SERVER["HTTP_REFERER"], "this")) {exit;} //him
if (strstr($_SERVER["HTTP_REFERER"], "that")) {exit;} //her
if (strstr($_SERVER["HTTP_REFERER"], "da other ting")) {exit;} //it

Comments/suggestions?

oesxyl
12-24-2007, 06:45 AM
$iparray = array("66.231.188.156", "208.99.195.54", "38.100.41.101", "69.41.13.5");
if(in_array($_SERVER["REMOTE_ADDR"],$iparray)){ exit; }


best regards

ArcticFox
12-24-2007, 06:57 AM
Thanks, oesxyl.

Is that an "if it is" or "if it contains" script? Sometimes I have to filter out whole IP ranges, or have to filter out an example like this:



if (strstr($_SERVER["REMOTE_ADDR"], "natcrawlbloc01")) {exit;}
if (strstr($_SERVER["HTTP_X_FORWARDED_FOR"], "74.105.141")) {exit;}


Can you tell me if your script will work for these?

Thank you! Happy Holidays!

Inigoesdr
12-24-2007, 07:09 AM
You would have to make a separate array for the other header(or use the same array with a separate if switch).

oesxyl
12-24-2007, 08:13 AM
Thanks, oesxyl.

Is that an "if it is" or "if it contains" script? Sometimes I have to filter out whole IP ranges, or have to filter out an example like this:


if(in_array(x,y)) is same as "if x is one of items from y" and to be true y must be an array, and x must be exactly as one of the array items.

http://www.php.net/manual/en/function.in-array.php

It don't work for a IP ranges, because x must be exactly as one of the items from x array.



if (strstr($_SERVER["REMOTE_ADDR"], "natcrawlbloc01")) {exit;}

strstr nor in_array don't work for this because, as far as I know, $_SERVER["REMORE_ADDR"] is a IP therefor comparation with host name in both function return false, and therefor exit is never executed.



if (strstr($_SERVER["HTTP_X_FORWARDED_FOR"], "74.105.141")) {exit;}


this must work, same explanation as above but this time could return true.

With pleasure, and Happy Holidays!

ArcticFox
12-24-2007, 09:38 AM
I'm not following. So your code would not work with IP ranges?:


$iparray = array("66.231.188.", "208.99.", "38.100.41", "69.41.13.5");
if(in_array($_SERVER["REMOTE_ADDR"],$iparray)){ exit; }

If not, then I'm guessing these wouldn't work, either?:



$uaarray = array("66.231.188.", "208.99.", "38.100.41", "69.41.13.5");
if(in_array($_SERVER["REMOTE_ADDR"],$uaarray)){ exit; }

$prarray = array("Windows 95", "Gigabot", "Check&Get", "Java");
if(in_array($_SERVER["HTTP_USER_AGENT"],$uaarray)){ exit; }

$rearray = array("google", "mail.yahoo.com", "dogpile.com", "coding");
if(in_array($_SERVER["HTTP_REFERER"],$uaarray)){ exit; }

abduraooft
12-24-2007, 10:01 AM
$iparray = array("66.231.188.", "208.99.", "38.100.41", "69.41.13.5");
if(in_array($_SERVER["REMOTE_ADDR"],$iparray)){ exit; }



$iparray = array("66.231.188.", "208.99.", "38.100.41", "69.41.13.5");
foreach($iparray as $ip)
{
if(strstr($_SERVER["REMOTE_ADDR"],$ip))
exit;
}




$prarray = array("Windows 95", "Gigabot", "Check&Get", "Java");
if(in_array($_SERVER["HTTP_USER_AGENT"],$uaarray)){ exit; }

$rearray = array("google", "mail.yahoo.com", "dogpile.com", "coding");
if(in_array($_SERVER["HTTP_REFERER"],$uaarray)){ exit; }


typo?

Inigoesdr
12-24-2007, 10:22 AM
I'm not following. So your code would not work with IP ranges?:


$iparray = array("66.231.188.", "208.99.", "38.100.41", "69.41.13.5");
if(in_array($_SERVER["REMOTE_ADDR"],$iparray)){ exit; }

If not, then I'm guessing these wouldn't work, either?:



$uaarray = array("66.231.188.", "208.99.", "38.100.41", "69.41.13.5");
if(in_array($_SERVER["REMOTE_ADDR"],$uaarray)){ exit; }

$prarray = array("Windows 95", "Gigabot", "Check&Get", "Java");
if(in_array($_SERVER["HTTP_USER_AGENT"],$uaarray)){ exit; }

$rearray = array("google", "mail.yahoo.com", "dogpile.com", "coding");
if(in_array($_SERVER["HTTP_REFERER"],$uaarray)){ exit; }


No, none of those would work. You're either going to have to use regular expressions to match ranges or parts of hostnames, or have a fixed number of octets you get from the IP before checking it against the array.
ie.

$octets = explode('.', $_SERVER['REMOTE_ADDR']);
if(in_array($octets[0] . '.' . $octets[1], $uaarray)
die('etc..');

oesxyl
12-24-2007, 07:44 PM
I'm not following. So your code would not work with IP ranges?:


$iparray = array("66.231.188.", "208.99.", "38.100.41", "69.41.13.5");
if(in_array($_SERVER["REMOTE_ADDR"],$iparray)){ exit; }


did not work in this case. It work only if $_SERVER["REMOTE_ADDR"] is "69.41.13.5", because is full IP.



If not, then I'm guessing these wouldn't work, either?:



$uaarray = array("66.231.188.", "208.99.", "38.100.41", "69.41.13.5");
if(in_array($_SERVER["REMOTE_ADDR"],$uaarray)){ exit; }


did not work, too
In both this cases you can use the solution posted by abduraooft





$prarray = array("Windows 95", "Gigabot", "Check&Get", "Java");
if(in_array($_SERVER["HTTP_USER_AGENT"],$uaarray)){ exit; }



I don't know if it work because but I presume is not working, I don't know if the strstr solution work, but if it work, you can use abduraooft solution too.





$rearray = array("google", "mail.yahoo.com", "dogpile.com", "coding");
if(in_array($_SERVER["HTTP_REFERER"],$uaarray)){ exit; }



this not work with in_array, but I don't think it work with strstr too.

see the corection from abduraooft post about uaarray.

best regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum