Jump to content

Archived

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

Hardbyte

in_array & strstr (Bots)

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

Share this post


Link to post
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.

Share this post


Link to post
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@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); 

};
[/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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Hi-ya,

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

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

Just waiting to be crawled lol

Many Thanks

Share this post


Link to post
Share on other sites

×

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.