Stop script running for spiders/bots

Discussion in 'PHP' started by adzeds, Feb 2, 2010.

  1. #1
    I am creating a small script to collect web stats and do not want it to run when spiders or bots are crawling my site.

    At the moment I have lots of records in my database that are hits from spiders and bots and I am not interested in them.

    Is there a PHP function I can use to determine if the requester is a human or a spider/bot?
     
    adzeds, Feb 2, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    Try something like:

    For detecting if the visitor is a bot or not:

    <?php
    function is_bot(){
    //array of bot names
    $bots = array("Googlebot", "alexa", "msnbot");
    if(in_array($_SERVER['HTTP_USER_AGENT'], $bots)){
    return true;
    } else {
    return false;
    }
    }
    ?>
    PHP:
    Usage:

    <?php
    if(is_bot() == false){
    //the visitor is not a bot...
    } else {
    //the visitor is a bot...
    }
    ?>
    PHP:
     
    Last edited: Feb 2, 2010
    danx10, Feb 2, 2010 IP
  3. adzeds

    adzeds Well-Known Member

    Messages:
    1,209
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    100
    #3
    Many Thanks
     
    adzeds, Feb 2, 2010 IP
  4. poblo10

    poblo10 Peon

    Messages:
    135
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I would say just ad the script url to a robots.txt
    If you do not know about robots.txt google it
     
    poblo10, Feb 3, 2010 IP