jasonc310771 11-18-2006, 09:43 PM 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
boeing747fp 11-18-2006, 10:04 PM $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
}
}
jasonc310771 11-18-2006, 10:10 PM Hi,
Thank you for your reply.
I think I understand this, not very clued up on arrays.
Thanks again.
jasonc310771 11-18-2006, 10:12 PM 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 ?
jasonc310771 11-18-2006, 10:19 PM 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
boeing747fp 11-18-2006, 10:23 PM 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'...
boeing747fp 11-18-2006, 10:24 PM try this
$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);
}
}
jasonc310771 11-18-2006, 10:29 PM 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
boeing747fp 11-18-2006, 10:33 PM you need to keep the if(count($matches) >0){....} INSIDE the foreach(){....} loop...
the /whatever/i is to make it Case Insensitive
jasonc310771 11-18-2006, 10:40 PM 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.
jasonc310771 11-18-2006, 10:50 PM 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.
boeing747fp 11-18-2006, 10:58 PM because your "go ogle" is not "google"
jasonc310771 11-18-2006, 11:07 PM 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
boeing747fp 11-18-2006, 11:10 PM ok i think i get it now
$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);
}
jasonc310771 11-18-2006, 11:12 PM 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.
boeing747fp 11-18-2006, 11:13 PM ^^ edited my above post.
jasonc310771 11-18-2006, 11:20 PM the follow produces this....
a bota botnot a bot
on the screen
which means it is still increase if the bot is found.
<?
$subject = "is google a bot slurp";//$_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.
echo("a bot");
}
}
if($num_bots == 0){
// increase counter if none of the words are in HTTP_USER_AGENT
echo("not a bot");
}
?>
boeing747fp 11-18-2006, 11:22 PM why did you put...
echo("a bot")
...right where i put "no bot" in comments??
and why are you putting spaces inside your "go ogle" and "slu rp" ??
jasonc310771 11-18-2006, 11:28 PM i put space in there to test if it is or is not a bot,
are sorry yes i did, i copied that line from another file thought i had copied another line.
but yes the code does work.
thank you so much for your help.
thank you for you patients
|
|