Jump to content

in_array & strstr (Bots)


Hardbyte

Recommended Posts

Hi-ya.

To start with, Iv got the below code:-

[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.

}

[/code]

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 [b]BOT[/b]S or STUPID[b]BOT[/b]S ?

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
Link to comment
https://forums.phpfreaks.com/topic/13719-in_array-strstr-bots/
Share on other sites

[code=php:0]$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.
};[/code]
[hr]

Hope it helps :)

Orio.
Link to comment
https://forums.phpfreaks.com/topic/13719-in_array-strstr-bots/#findComment-53271
Share on other sites

Hi-ya, thanks for the reply.

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

[code]
$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 protected]>";
$message = $agent." has visited ".$_SERVER['REQUEST_URI']."\n\nArray Bot ";
mail("[email protected]", $subject, $message, $headers);

}else{

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

};
[/code]

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 GOOGLE[b]BOT[/b], [b]BOT[/b]S, BOTa[b]CRAWLER[/b] etc..

Many thanks.
Link to comment
https://forums.phpfreaks.com/topic/13719-in_array-strstr-bots/#findComment-53285
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.