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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Regular Coder
    Join Date
    Mar 2006
    Posts
    478
    Thanks
    3
    Thanked 0 Times in 0 Posts

    check variable against an array of words

    Hi,

    I wish to check a variable for a number of words that are in a array.

    Not sure if this is the best way to do it, but this is what i have.

    I have my own counter system that counts every visitor, but some of these visits are from BOTS, spiders..

    So i wish to have a method of searching the HTTP_USER_AGENT for certain words i put in a array.

    What i am after is how i get the words in an array and how i then get the script to check the HTTP_USER_AGENT for a word in the array and if so it will not complete the script. (not add to the counter)
    if none of the words are in the HTTP_USER_AGENT then it will add to the counter.

    so something like?

    <?
    // do array

    // check if HTTP_USER_AGENT contains a word from array
    if () {
    // increase counter if none of the words are in HTTP_USER_AGENT
    include(increase.php);
    }

    ?>


    its the bit between the if () that i have no idea about, any help would be great.

    cheers in advance for your help.

    Jason

  • #2
    Regular Coder
    Join Date
    Oct 2003
    Posts
    603
    Thanks
    2
    Thanked 1 Time in 1 Post
    PHP Code:
    $subject $_SERVER['HTTP_USER_AGENT'];
    $bots = array("/google/i","/slurp/i");
    foreach(
    $bots as $num=>$bot){
        
    preg_match($bot$subject$matches);
        if(
    count($matches) >0){
            
    //Found a match!
        
    }else{
            
    //No Match
        
    }

    Last edited by boeing747fp; 11-18-2006 at 10:07 PM.

  • #3
    Regular Coder
    Join Date
    Mar 2006
    Posts
    478
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Hi,

    Thank you for your reply.

    I think I understand this, not very clued up on arrays.

    Thanks again.

  • #4
    Regular Coder
    Join Date
    Mar 2006
    Posts
    478
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Ah, just looking again, i did not see something..

    where do i put the text to be searched? is the text to be searched contained in the variable matches ?

  • #5
    Regular Coder
    Join Date
    Mar 2006
    Posts
    478
    Thanks
    3
    Thanked 0 Times in 0 Posts
    just put it in a text page to test it out, and added a 'found' and 'not found' echo statements.

    but it shows both

    am i missing something?

    please advise.

    thanks

  • #6
    Regular Coder
    Join Date
    Oct 2003
    Posts
    603
    Thanks
    2
    Thanked 1 Time in 1 Post
    well, it will check each piece of the array of words you are checking for, and if it finds a match, it will say 'found' otherwise it will say 'not found'... if you have more than 1 word you are searching for, you will have that many results, whether 2 'found's or 2 'not found's or 1 'found' and 1 'not found'...

  • #7
    Regular Coder
    Join Date
    Oct 2003
    Posts
    603
    Thanks
    2
    Thanked 1 Time in 1 Post
    try this
    PHP Code:
    $subject $_SERVER['HTTP_USER_AGENT']; 
    $bots = array("google","slurp"); 
    foreach(
    $bots as $num=>$bot){ 
        
    preg_match("/".$bot."/i"$subject$matches); 
        if(
    count($matches) >0){ 
            
    //Found a match! 
        
    }else{ 
            
    // increase counter if none of the words are in HTTP_USER_AGENT
            
    include(increase.php); 
        } 


  • #8
    Regular Coder
    Join Date
    Mar 2006
    Posts
    478
    Thanks
    3
    Thanked 0 Times in 0 Posts
    ah yes i see you changed it, thanks..

    i have changed mine and see it does now work.

    i see that it show the 'found' statement everytime something is found. so i have changed it to this, but i have now got another problem.


    <?
    $subject = "is google a bot"; //$_SERVER['HTTP_USER_AGENT'];
    $bots = array("/google/i","/slurp/i");
    foreach($bots as $num=>$bot){
    preg_match($bot, $subject, $matches);
    }

    if(count($matches) >0){
    //Found a match!
    echo("found");
    }
    ?>

    i have changed the top line to test the script, but i do not know what the /i is for in the $bots array, please advise what these are for.

    thank you again for your help.

    J

  • #9
    Regular Coder
    Join Date
    Oct 2003
    Posts
    603
    Thanks
    2
    Thanked 1 Time in 1 Post
    you need to keep the if(count($matches) >0){....} INSIDE the foreach(){....} loop...

    the /whatever/i is to make it Case Insensitive

  • #10
    Regular Coder
    Join Date
    Mar 2006
    Posts
    478
    Thanks
    3
    Thanked 0 Times in 0 Posts
    ok i see, yes i had not thought of that, checking the case of the letters too.

    when i put the code in a test file to test it out i get the 'found' printed for each of the words found, which would mean that the counter that is just meant for counting each visit but a human, would be increase for each of the words found, this is why i thought of putting it outside, but as you can see i am still learning php and still need a bit of guidence.

  • #11
    Regular Coder
    Join Date
    Mar 2006
    Posts
    478
    Thanks
    3
    Thanked 0 Times in 0 Posts
    ok i have got this far...

    <?
    $subject = "is google a bot slurp";//$_SERVER['HTTP_USER_AGENT'];
    $bots = array("google","slurp");
    foreach($bots as $num=>$bot){
    preg_match("/".$bot."/i", $subject, $matches);
    if(count($matches) >0){
    //Found a match!
    echo("bot");
    }else{
    // increase counter if none of the words are in HTTP_USER_AGENT
    echo("increase");
    }
    }
    ?>

    produces this...


    'botbot' on the screen, which means its a bot so the visitor counter will not increase.

    but this....



    <?
    $subject = "is go ogle a bot slurp";//$_SERVER['HTTP_USER_AGENT'];
    $bots = array("google","slurp");
    foreach($bots as $num=>$bot){
    preg_match("/".$bot."/i", $subject, $matches);
    if(count($matches) >0){
    //Found a match!
    echo("bot");
    }else{
    // increase counter if none of the words are in HTTP_USER_AGENT
    echo("increase");
    }
    }
    ?>


    produces this...




    'increasebot' on the screen

    which increases the visitor counter even though one of the words is in the subject string.

  • #12
    Regular Coder
    Join Date
    Oct 2003
    Posts
    603
    Thanks
    2
    Thanked 1 Time in 1 Post
    because your "go ogle" is not "google"

  • #13
    Regular Coder
    Join Date
    Mar 2006
    Posts
    478
    Thanks
    3
    Thanked 0 Times in 0 Posts
    i think i maybe sounding as though i want to count the number of words for the counter. but this is not what i needed, sorry if this is what it sounded like.

    below is i think the best i can do to show what i am after.

    thank you for your help.

    J


    <?
    // subject contains the string of httpuseragent
    $subject = "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)";

    // the array of words to search
    $bots = array("google","slurp");

    // reset found variable
    $found = "";

    // start search

    // search for each word in $bot array and if found set a new variable lets say $found = "found"

    // end search

    // if ($found = "found") {
    include("increasecounter.php");
    }

    // end

  • #14
    Regular Coder
    Join Date
    Oct 2003
    Posts
    603
    Thanks
    2
    Thanked 1 Time in 1 Post
    ok i think i get it now

    PHP Code:
    $subject $_SERVER['HTTP_USER_AGENT'];  
    $num_bots=0;
    $bots = array("google","slurp");  //all the bots you are searching to exclude from the counter
    foreach($bots as $num=>$bot){  
        
    preg_match("/".$bot."/i"$subject$matches);  
        if(
    count($matches) >0){  
            
    //Found a match! DO NOT INCREASE
            
    $num_bots++;
        }else{  
            
    //no bot.
        
    }  

    if(
    $num_bots == 0){
            
    // increase counter if none of the words are in HTTP_USER_AGENT 
            
    include(increase.php);  

    Last edited by boeing747fp; 11-18-2006 at 11:12 PM.

  • #15
    Regular Coder
    Join Date
    Mar 2006
    Posts
    478
    Thanks
    3
    Thanked 0 Times in 0 Posts
    no sorry not when the bot views.

    when a bot view the counter does not increase.

    when a human views it should not have any of the words i place in the BOT variable, so in this case the human visitor counter would then need increasing.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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