How can we get Browser Name and Version information using php script?
<?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.
See get_browser()
.
<?php
echo $_SERVER['HTTP_USER_AGENT'] . "\n\n";
$browser = get_browser(null, true);
print_r($browser);
?>
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/
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™.
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.