Jump to content

Determining browser or bot from user agent -


g1c9

Recommended Posts

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:

[code]
    $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;
[/code]

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

[!--quoteo(post=349353:date=Feb 25 2006, 07:06 PM:name=cameron)--][div class=\'quotetop\']QUOTE(cameron @ Feb 25 2006, 07:06 PM) [snapback]349353[/snapback][/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.

[code]

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';
}
[/code]

hope it helps
Link to comment
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.