g1c9 Posted February 25, 2006 Share Posted February 25, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/3580-determining-browser-or-bot-from-user-agent/ Share on other sites More sharing options...
g1c9 Posted February 25, 2006 Author Share Posted February 25, 2006 No help? :( Quote Link to comment https://forums.phpfreaks.com/topic/3580-determining-browser-or-bot-from-user-agent/#findComment-12439 Share on other sites More sharing options...
kenrbnsn Posted February 25, 2006 Share Posted February 25, 2006 Here's how I check for similar items:[code]<?php if (preg_match('/slurp|grub|[Bb]ot|archiver|NetMonitor/',$_SERVER['HTTP_USER_AGENT'])) {?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/3580-determining-browser-or-bot-from-user-agent/#findComment-12441 Share on other sites More sharing options...
g1c9 Posted February 25, 2006 Author Share Posted February 25, 2006 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 Link to comment https://forums.phpfreaks.com/topic/3580-determining-browser-or-bot-from-user-agent/#findComment-12446 Share on other sites More sharing options...
Squirrel*Salad Posted February 26, 2006 Share Posted February 26, 2006 [!--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 Quote Link to comment https://forums.phpfreaks.com/topic/3580-determining-browser-or-bot-from-user-agent/#findComment-12552 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.