Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder ArcticFox's Avatar
    Join Date
    Jan 2004
    Location
    Vostok Station, AQ
    Posts
    602
    Thanks
    35
    Thanked 3 Times in 3 Posts

    Question An easier way to write this filter?

    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.

    PHP Code:
    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 
    PHP Code:
    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? 
    PHP Code:
    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?
    Last edited by ArcticFox; 12-24-2007 at 04:55 AM.
    <div> - putting your mind in a box since 1997

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    PHP Code:
    $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

  • Users who have thanked oesxyl for this post:

    ArcticFox (12-24-2007)

  • #3
    Regular Coder ArcticFox's Avatar
    Join Date
    Jan 2004
    Location
    Vostok Station, AQ
    Posts
    602
    Thanks
    35
    Thanked 3 Times in 3 Posts
    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:

    PHP Code:
    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!
    <div> - putting your mind in a box since 1997

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    You would have to make a separate array for the other header(or use the same array with a separate if switch).

  • #5
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by ArcticFox View Post
    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.

    PHP Code:
    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.

    PHP Code:
    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!

  • #6
    Regular Coder ArcticFox's Avatar
    Join Date
    Jan 2004
    Location
    Vostok Station, AQ
    Posts
    602
    Thanks
    35
    Thanked 3 Times in 3 Posts
    I'm not following. So your code would not work with IP ranges?:

    PHP Code:
    $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?:

    PHP Code:
    $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; } 
    <div> - putting your mind in a box since 1997

  • #7
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,801
    Thanks
    160
    Thanked 2,216 Times in 2,203 Posts
    Blog Entries
    1
    Quote Originally Posted by ArcticFox View Post
    $iparray = array("66.231.188.", "208.99.", "38.100.41", "69.41.13.5");
    if(in_array($_SERVER["REMOTE_ADDR"],$iparray)){ exit; }
    PHP Code:
    $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;
     } 
    Quote Originally Posted by ArcticFox View Post

    $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?
    Last edited by abduraooft; 12-24-2007 at 09:05 AM.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #8
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    Quote Originally Posted by ArcticFox View Post
    I'm not following. So your code would not work with IP ranges?:

    PHP Code:
    $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?:

    PHP Code:
    $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.
    PHP Code:
    $octets explode('.'$_SERVER['REMOTE_ADDR']);
    if(
    in_array($octets[0] . '.' $octets[1], $uaarray)
        die(
    'etc..'); 

  • #9
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by ArcticFox View Post
    I'm not following. So your code would not work with IP ranges?:

    PHP Code:
    $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?:

    PHP Code:
    $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

    PHP Code:
    $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.

    PHP Code:
    $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
    Last edited by oesxyl; 12-24-2007 at 06:57 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •