s1yman Posted February 13, 2009 Share Posted February 13, 2009 Hi, I made a simple script to check what browser some one is using, at the moment it just displays what browser it is on the page, but I'm having problems with opera. My code is; <?php $browserinfo=$_SERVER['HTTP_USER_AGENT'] ; print $browserinfo; echo "<br />"; /*make lowercase*/ $browserstr=strtolower($browserinfo); echo $browserstr; echo "<br />"; /*If string contains "firefox" set type to firefox, if not check for other browsers*/ if (strpos($browserstr,"firefox")) $browsertype="firefox"; elseif (strpos($browserstr,"opera")) $browsertype="opera"; elseif (strpos($browserstr,"msie")) $browsertype="msie"; elseif (strpos($browserstr,"safari")) $browsertype="safari"; /*If none of the above set type to other*/ else $browsertype="other"; echo $browsertype; ?> browserstr displays "opera/9.63 (windows nt 5.1; u; en) presto/2.1.1" however, browsertype displays "other" it works fine for firefox, explorer and safari. Probably did something simple wrong. Thanks in advance for any help. Link to comment https://forums.phpfreaks.com/topic/145068-solved-strpos-problem/ Share on other sites More sharing options...
printf Posted February 13, 2009 Share Posted February 13, 2009 Use stripos(); example... <?php function get_agent () { $agent = 'other'; if ( false !== stripos ( $_SERVER['HTTP_USER_AGENT'], 'safari' ) ) { $agent = 'safari'; } else if ( false !== stripos ( $_SERVER['HTTP_USER_AGENT'], 'opera' ) ) { $agent = 'opera'; } else if ( false !== stripos ( $_SERVER['HTTP_USER_AGENT'], 'firefox' ) ) { $agent = 'firefox'; } else if ( false !== stripos ( $_SERVER['HTTP_USER_AGENT'], 'msie' ) ) { $agent = 'msie'; } else if ( false !== stripos ( $_SERVER['HTTP_USER_AGENT'], 'mozilla/4' ) || false !== stripos ( $_SERVER['HTTP_USER_AGENT'], 'mozilla/5' ) ) { $agent = 'mozilla'; } return $agent; } /* usage */ echo get_agent (); ?> Link to comment https://forums.phpfreaks.com/topic/145068-solved-strpos-problem/#findComment-761207 Share on other sites More sharing options...
s1yman Posted February 13, 2009 Author Share Posted February 13, 2009 kool, i'll try that. thanks Link to comment https://forums.phpfreaks.com/topic/145068-solved-strpos-problem/#findComment-761208 Share on other sites More sharing options...
s1yman Posted April 12, 2009 Author Share Posted April 12, 2009 Incase any ever wants to know in future ... my original script didn't work because Opera is labelled as "Presto" no Opera like you'd expect Link to comment https://forums.phpfreaks.com/topic/145068-solved-strpos-problem/#findComment-808138 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.