djpic Posted April 17, 2008 Share Posted April 17, 2008 I have a question about the get browser function. I am currently using the $_SERVER['HTTP_USER_AGENT'] but that just outputs a long variable: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7) Gecko/20040803 Firefox/0.9.3 << example All I need is the browser name and version number. Formated like this if possible: IE 7, Firefox 2, etc. I read up on the get_browser function but I want to know how I can get the information from just that browser. The documentation says it returns an array: Array ( [browser_name_regex] => ^mozilla/5\.0 (windows; .; windows nt 5\.1; .*rv:.*) gecko/.* firefox/0\.9.*$ [browser_name_pattern] => Mozilla/5.0 (Windows; ?; Windows NT 5.1; *rv:*) Gecko/* Firefox/0.9* [parent] => Firefox 0.9 [platform] => WinXP [browser] => Firefox [version] => 0.9 [majorver] => 0 [minorver] => 9 [css] => 2 [frames] => 1 [iframes] => 1 [tables] => 1 [cookies] => 1 [backgroundsounds] => [vbscript] => [javascript] => 1 [javaapplets] => 1 [activexcontrols] => [cdf] => [aol] => [beta] => 1 [win16] => [crawler] => [stripper] => [wap] => [netclr] => ) But how do I just pull the [browser], [version], [platform] from that statement? Here is the documentation for the get_browser function: http://us3.php.net/manual/en/function.get-browser.php Thanks. Quote Link to comment Share on other sites More sharing options...
conker87 Posted April 17, 2008 Share Posted April 17, 2008 I think: <?php echo $_SERVER['HTTP_USER_AGENT']['browser']; echo $_SERVER['HTTP_USER_AGENT']['version']; echo $_SERVER['HTTP_USER_AGENT']['platform']; ?> Quote Link to comment Share on other sites More sharing options...
discomatt Posted April 17, 2008 Share Posted April 17, 2008 $browser = get_browser(); echo $browser->browser .'<br>'. $browser->version .'<br>'. $browser->platform; From what I've read this function should be avoided because it it slow. Quote Link to comment Share on other sites More sharing options...
djpic Posted April 17, 2008 Author Share Posted April 17, 2008 I think: <?php echo $_SERVER['HTTP_USER_AGENT']['browser']; echo $_SERVER['HTTP_USER_AGENT']['version']; echo $_SERVER['HTTP_USER_AGENT']['platform']; ?> I just get: MMM when I do that. Quote Link to comment Share on other sites More sharing options...
djpic Posted April 17, 2008 Author Share Posted April 17, 2008 $browser = get_browser(); echo $browser->browser .'<br>'. $browser->version .'<br>'. $browser->platform; From what I've read this function should be avoided because it it slow. That doesn't return anything. Quote Link to comment Share on other sites More sharing options...
discomatt Posted April 17, 2008 Share Posted April 17, 2008 Error reporting is your friend. Try this: <?php if( !ini_get('browscap') ) echo 'Can\'t use get_browser with current ini settings'; else { $browser = get_browser(); echo $browser->browser .'<br>'. $browser->version .'<br>'. $browser->platform; } ?> Quote Link to comment Share on other sites More sharing options...
thebadbad Posted April 17, 2008 Share Posted April 17, 2008 Keep in mind that get_browser() loads the ~ 200-400kb browscap.ini file each time, thus slowing down the script a great deal. And newer browsers will only be recognized if the browscap.ini is up to date (that requires a lot of updating from your side). However, IF you go with it, you should not run i more than once per user/session - if you need the output on multiple pages, I would store it in a session. Quote Link to comment Share on other sites More sharing options...
djpic Posted April 17, 2008 Author Share Posted April 17, 2008 Error reporting is your friend. Try this: <?php if( !ini_get('browscap') ) echo 'Can\'t use get_browser with current ini settings'; else { $browser = get_browser(); echo $browser->browser .'<br>'. $browser->version .'<br>'. $browser->platform; } ?> Ok, Can't use get_browser with current ini settings, how do I fix that. I have full access to the server. I am running Fedora 8. Quote Link to comment Share on other sites More sharing options...
djpic Posted April 17, 2008 Author Share Posted April 17, 2008 Keep in mind that get_browser() loads the ~ 200-400kb browscap.ini file each time, thus slowing down the script a great deal. And newer browsers will only be recognized if the browscap.ini is up to date (that requires a lot of updating from your side). However, IF you go with it, you should not run i more than once per user/session - if you need the output on multiple pages, I would store it in a session. Is there any other way to get that information with placing that load on the server? Quote Link to comment Share on other sites More sharing options...
discomatt Posted April 17, 2008 Share Posted April 17, 2008 Is there any other way to get that information with placing that load on the server? Use regex to parse the HTTP_USER_AGENT variable. Ok, Can't use get_browser with current ini settings, how do I fix that. I have full access to the server. I am running Fedora 8. There's plenty out there that tells you how to fix that.. just look up browscap http://garetjax.info/projects/browscap/ Quote Link to comment Share on other sites More sharing options...
djpic Posted April 18, 2008 Author Share Posted April 18, 2008 That doesn't tell me how to install it into the php / apache system and doesn't have any example codes with the get_browser function. Quote Link to comment Share on other sites More sharing options...
djpic Posted April 18, 2008 Author Share Posted April 18, 2008 Never-mind, I got it, thanks for all the help! This topic is SOLVED! Quote Link to comment 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.