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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.