Jump to content


Photo

Determining browser or bot from user agent -


  • Please log in to reply
4 replies to this topic

#1 g1c9

g1c9
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 25 February 2006 - 01:21 AM

Hey,

I am trying to take the user agent and determine weather it is a browser or a bot on my site with PHP and the user agent. I am doing this by searching the user agent for common browsers/renderers.

Here is the code I have so far:

    $agent = getenv("HTTP_USER_AGENT");
    echo '<b>USER AGENT:</b> '.$agent.'<br><br>';
    $x = 0;
    $hide = array('Mozilla','MSIE','Opera','Lynx','WebTV','MCSA','Mosaic');
    foreach ($hide as $find) {
        if (strpos($agent, $find) === false) {
            $is_bot[$x] = true;
            }
            echo $find.': '.$is_bot[$x].'<br>';
            $x++;
        }
    $is_bot = false;

I wish I could tell you all the problem so you could fix it, but I don't know the problem! The code above outputs nothing for the first one, (mozilla), and 1's for everything after that!

Please help me debug this!

Thanks in advance,

Cameron

#2 g1c9

g1c9
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 25 February 2006 - 05:54 PM

No help? :(

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 25 February 2006 - 06:25 PM

Here's how I check for similar items:
<?php 
if (preg_match('/slurp|grub|[Bb]ot|archiver|NetMonitor/',$_SERVER['HTTP_USER_AGENT'])) {
?>

Ken


#4 g1c9

g1c9
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 25 February 2006 - 07:06 PM

That checks if it is a bot, correct?

Does this work well for most SE bots? (eg: google, yahoo, msn)

Any browsers this may cuase a problem for?

#5 Squirrel*Salad

Squirrel*Salad
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 26 February 2006 - 09:01 AM

[!--quoteo(post=349353:date=Feb 25 2006, 07:06 PM:name=cameron)--][div class=\'quotetop\']QUOTE(cameron @ Feb 25 2006, 07:06 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
That checks if it is a bot, correct?

Does this work well for most SE bots? (eg: google, yahoo, msn)

Any browsers this may cuase a problem for?
[/quote]

I use the following on my site to determin browser type which also detects bots ( google, slurp etc )
you can modify it for your own needs.


if ((ereg('Nav', $HTTP_SERVER_VARS['HTTP_USER_AGENT'])) || (ereg('Gold', $HTTP_SERVER_VARS['HTTP_USER_AGENT'])) || (ereg('X11', $HTTP_SERVER_VARS['HTTP_USER_AGENT'])) || (ereg('Mozilla', $HTTP_SERVER_VARS['HTTP_USER_AGENT'])) || (ereg('Netscape', $HTTP_SERVER_VARS['HTTP_USER_AGENT'])) AND (!ereg('MSIE', $HTTP_SERVER_VARS['HTTP_USER_AGENT'])) AND (!ereg('Konqueror', $HTTP_SERVER_VARS['HTTP_USER_AGENT'])) AND (!ereg('Yahoo', $HTTP_SERVER_VARS['HTTP_USER_AGENT'])) AND (!ereg('Firefox', $HTTP_SERVER_VARS['HTTP_USER_AGENT'])))
{
    $browser = 'Netscape';
} 
elseif( ereg('Firefox', $_SERVER['HTTP_USER_AGENT']) ) 
{
    $browser = 'FireFox';
}
elseif( ereg('MSIE', $HTTP_SERVER_VARS['HTTP_USER_AGENT']) ) 
{
    $browser = 'MSIE';
}
elseif( ereg('Lynx', $HTTP_SERVER_VARS['HTTP_USER_AGENT']) )
{
    $browser = 'Lynx';
} 
elseif( ereg('Opera', $HTTP_SERVER_VARS['HTTP_USER_AGENT']) ) 
{
    $browser = 'Opera';
}
elseif( ereg('WebTV', $HTTP_SERVER_VARS['HTTP_USER_AGENT']) ) 
{
    $browser = 'WebTV';
}
elseif( ereg('Konqueror', $HTTP_SERVER_VARS['HTTP_USER_AGENT'])) 
{
    $browser = 'Konqueror';
}
elseif( ( eregi('bot', $HTTP_SERVER_VARS['HTTP_USER_AGENT']) ) || ( ereg('Google', $HTTP_SERVER_VARS['HTTP_USER_AGENT']) ) || ( ereg('Slurp', $HTTP_SERVER_VARS['HTTP_USER_AGENT']) ) || ( ereg('Scooter', $HTTP_SERVER_VARS['HTTP_USER_AGENT']) ) || ( eregi('Spider', $HTTP_SERVER_VARS['HTTP_USER_AGENT']) ) || ( eregi('Infoseek', $HTTP_SERVER_VARS['HTTP_USER_AGENT']) ) ) 
{
    $browser = 'Bot';
}
else
{
    $browser = 'Other';
} 

hope it helps




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users