1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Parse Out Bot's By User Agent

Discussion in 'PHP' started by codytaylor, Jun 14, 2009.

  1. codytaylor

    codytaylor New Member

    Joined:
    Jun 14, 2009
    Messages:
    1
    Likes Received:
    1
    Trophy Points:
    0
    Home Page:
    Useful function for logging stats and visitors in php.
    PHP:
    //returns 1 if the user agent is a bot
    function is_bot($user_agent)
    {
      
    //if no user agent is supplied then assume it's a bot
      
    if($user_agent == "")
        return 
    1;

      
    //array of bot strings to check for
      
    $bot_strings = Array(  "google",     "bot",
                
    "yahoo",     "spider",
                
    "archiver",   "curl",
                
    "python",     "nambu",
                
    "twitt",     "perl",
                
    "sphere",     "PEAR",
                
    "java",     "wordpress",
                
    "radian",     "crawl",
                
    "yandex",     "eventbox",
                
    "monitor",   "mechanize",
                
    "facebookexternal"
              
    );
      foreach(
    $bot_strings as $bot)
      {
        if(
    strpos($user_agent,$bot) !== false)
        { return 
    1; }
      }
      
      return 
    0;
    }

     
    shabbir likes this.
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
    Nice little utility
     
  3. LenoxFinlay

    LenoxFinlay Banned

    Joined:
    Apr 15, 2009
    Messages:
    46
    Likes Received:
    0
    Trophy Points:
    0
    I insert at the very end of the page after I flush everything out so there will be no delay of content even if the db is bogged down. And if I was give it to someone else who is using iis I would have to recode the entire thing. Also when doing reporting on large sets of data I don’t want to have to parse huge log files for every report.
     

Share This Page