tags:

views:

168

answers:

6

How can we get Browser Name and Version information using php script?

+5  A: 
<?php 
 echo $_SERVER['HTTP_USER_AGENT'];
 ?> 

As Palantir says, additionally have a look at the get_browser function, where you can check also capabilities enabled in the browser.

pakore
Keeping in mind that the user agent is easily spoofable.
nico
+2  A: 

See get_browser().

<?php
echo $_SERVER['HTTP_USER_AGENT'] . "\n\n";

$browser = get_browser(null, true);
print_r($browser);
?>
Andy E
+1  A: 

You could always take a look at the php function get_browser http://php.net/manual/en/function.get-browser.php. You will need $_SERVER['HTTP_USER_AGENT'] for this.

You may also want to take a look at Chris Schuld Browser Detection Class. http://chrisschuld.com/projects/browser-php-detecting-a-users-browser-from-php/

Lizard
+2  A: 

All in all, you can't. You can certainly try to get it, and you're almost certainly guaranteed to get something that looks like what you want; but there is absolutely no way of checking wether or not the information is correct. When you receive a user agent string, the browser on the other end could be truthful, or it could be lying. When dealing with users, always assume that it is, in fact, lying.

There is no "best way" to deal with this, but what you most likely want to do is test your site with a wide variety of browsers, use portable HTML and CSS techniques, and if you absolutely must, fill the holes with JavaScript.

Choosing what data to send to a browser based on what browser you think it is, is a Bad Idea™.

Williham Totland
I disagree. Sometimes there are reasons to target certain browsers, because they sometimes have different features. The user's browser reports whatever it wants, and if the user wishes to change that, then he will get the wrong content and ultimately broken functionality. Users don't to that, and if they do, then they know what they are doing and what to expect. The entire web in fact is based on some kind of trust where the user has more power than when using desktop software, it's part of the game. Think about referrers, cookies, javascript support, etc...
Palantir
@Palantir: amen, brother.
nickf
Identity != capabilities. Check for features, not names.
Williham Totland
+1 good answer. PHP should deliver content independently of the browser. If you want to take into account different browser capabilities you can use a client-side language for that. And even there you should not rely on what the browser tells you, as @Williham Totland correctly say you should check for features not names.
nico
+1  A: 

You will need to create function to translate user agent data into common names of browsers

For example, $_SERVER['HTTP_USER_AGENT'] could return Mozilla/5.0 (Windows; ?; Windows NT 5.1; *rv:*) Gecko/* Firefox/0.9* is firefox or Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.55 Safari/533.4 is Chrome

The details provide you with the rendering engine, code base, version, os, etc...

I'd suggest using preg_match and/or a list of known browsers is you want to do something like

echo browserCommonName($_SERVER['HTTP_USER_AGENT']); to output "Google Chrome".

browserCommonName($userAgent) would need a list of known browsers.

edit: just noticed get_browser buit into php does this, my bad for not reading the thread.