Jump to content


Photo

in_array & strstr (Bots)


  • Please log in to reply
4 replies to this topic

#1 Hardbyte

Hardbyte
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts
  • LocationMidlands, UK

Posted 05 July 2006 - 09:27 AM

Hi-ya.

To start with, Iv got the below code:-


$agent = strtoupper($_SERVER['HTTP_USER_AGENT']);
$bots = array('GOOGLE','GOOGLEBOT','BOT');


if (in_array($agent, $bots)){
		
// do something here.

}else{

// do something else here.

}


I assume the above code is stating: if $agent == $bots (equal to BOT). But how do I make it so its *like* BOT, incase its really BOTS or STUPIDBOTS ?

Iv been playing around with strstr but it dont seem to work in this array style (unless Im doing it wrong).

Any help would be appreciated.

Thanks


#2 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 05 July 2006 - 11:25 AM

$agent = strtoupper($_SERVER['HTTP_USER_AGENT']);
$bots = array('GOOGLE','GOOGLEBOT','BOT');
$continue=0;
foreach($bots as $bot){
if(strstr($bot,$agent)!==FALSE){$continue=1;};
};

if(continue==1)
{
// do something here.
}else{
// do something else here.
};
 

Hope it helps :)

Orio.
Think you're smarty?

(Gone until 20 to November)

#3 Hardbyte

Hardbyte
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts
  • LocationMidlands, UK

Posted 05 July 2006 - 12:21 PM

Hi-ya, thanks for the reply.

Im afraid its still not working. Below is your new code implemented:-

$agent = strtoupper($_SERVER['HTTP_USER_AGENT']);

$bots = array('GOOGLE','GOOGLEBOT','BOT','CRAWLER');


$continue=0;
foreach($bots as $bot){
if(strstr($bot,$agent)!==FALSE){$continue=1;};
};

if($continue==1)
{	
//send mail (of bot array)
$subject = "Reported Bot";
$headers  = "From: email@addy.com>"; 
$message = $agent." has visited ".$_SERVER['REQUEST_URI']."\n\nArray Bot ";
mail("myemailaddy@domain.com", $subject, $message, $headers);

}else{

//send mail (all)
$subject = "Reported Bot";
$headers  = "From: <email@addy.com>"; 
$message = $agent." has visited ".$_SERVER['REQUEST_URI']."\n\nArray Bot: not in array ";
mail("myemailaddy@domain.com", $subject, $message, $headers);  

};

Im trying to determine which bots from my array are going to the site, it then emails me if it was a bot in my array or something else (like IE or Firefox) picking up on keywords such as BOT in GOOGLEBOT, BOTS, BOTaCRAWLER etc..

Many thanks.

#4 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 05 July 2006 - 12:28 PM

I am not sure if you mean to check (for example) if "GOOGLE" is inside $agent, or vice cersa- $agent is in "GOOGLE".

If it's "GOOGLE" inside $agent, this is how it's supposed to be:
if(strstr($agent,$bot)!==FALSE){$continue=1;};
(I changed $bot,$agent to $agent,$bot).

Orio.
Think you're smarty?

(Gone until 20 to November)

#5 Hardbyte

Hardbyte
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts
  • LocationMidlands, UK

Posted 05 July 2006 - 12:33 PM

Hi-ya,

I also just did the same but you replied before I got here  ;D

I changed:
if(strpos($agent,$bot)!==FALSE){$continue=1;};

Just waiting to be crawled lol

Many Thanks




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users