How to Detect the Browser with PHP or JavaScript

How can I detect the browser with PHP or JavaScript?

The best way to do this in JS I found is on Quirksmode. I made one for PHP which should work with common browsers :

  $browser = array(
'version' => '0.0.0',
'majorver' => 0,
'minorver' => 0,
'build' => 0,
'name' => 'unknown',
'useragent' => ''
);

$browsers = array(
'firefox', 'msie', 'opera', 'chrome', 'safari', 'mozilla', 'seamonkey', 'konqueror', 'netscape',
'gecko', 'navigator', 'mosaic', 'lynx', 'amaya', 'omniweb', 'avant', 'camino', 'flock', 'aol'
);

if (isset($_SERVER['HTTP_USER_AGENT'])) {
$browser['useragent'] = $_SERVER['HTTP_USER_AGENT'];
$user_agent = strtolower($browser['useragent']);
foreach($browsers as $_browser) {
if (preg_match("/($_browser)[\/ ]?([0-9.]*)/", $user_agent, $match)) {
$browser['name'] = $match[1];
$browser['version'] = $match[2];
@list($browser['majorver'], $browser['minorver'], $browser['build']) = explode('.', $browser['version']);
break;
}
}
}

What's the best way to detect a browser with php?

You can't reliably detect what the browser is. Its as simple as that.

Browsers are capable of lying about their identity, and frequently do.

Some proxies and security products strip the user agent data from the request, so your PHP code may just get an empty string.

In Javascript you may have a bit more luck, though it's still problematic, but at the PHP level you can never really be certain.

My first suggestion would be to drop support for IE6, unless your demographic is in the really stubbon minority. IE6 usage stats is down to below 2.5% in most developed countries now. That alone will get rid of a large part of your CSS problems. IE7 is still not good, but it's a clear mile better than IE6, and it is just about supportable while sticking to modern standards.

My second suggestion is rather than trying to downgrade your site for these browsers, try to upgrade the browser.

There are a number of very good hacks and tools that will allow you to improve CSS support in older versions of IE. I recommend you try the following:

  • CSS3Pie
  • Selectivizr
  • Modernizr
  • Dean Edwards' IE7.js

And of course, the ubiquitous JQuery.

Detecting the visitor's browser by JAVASCRIPT or PHP?

OK. So User-Agent header is not required by RFC

User agents SHOULD include this field with requests.

https://www.rfc-editor.org/rfc/rfc2616#section-14.43

Which means the server side detection is not guaranteed.

Similarly client side detection typically relies on navigator.userAgent but that is also provided by the user agent (browser or what not) and similarly cannot be guaranteed.

Thus the answer to your question is 50/50 :)

Now, if you are trying to figure out how to handle different browsers - feature detection is your safest bet here - but that's a different question ;)

Reliable browser detection with javascript?

(Skipping all the arguments on whether what you want to do is a good or a bad thing. Assuming you really like Firefox and want to promote it.)

  1. Use the navigator object to check if the browser is Firefox. navigator.appCodeName, navigator.appName, navigator.appVersion. What you want to do is not a critical part of your website, so you don't need a fail-safe detection method. If it works for over 95% of your targeted users than the method is good.

  2. I doubt you need permission to advertise a free product. Mozilla even encourages you to do so.

reliable user browser detection with php

Using an existing method (ie get_browser) is probably better than writing something yourself, since it has (better) support and will be updated with newer versions. There might be also usable libraries out there for getting the browser id in a reliable way.

Decoding the $_SERVER['HTTP_USER_AGENT'] is difficult, since a lot of browsers have quite similar data and tend to (mis)use it for their own benefits. But if you really want to decode them, you could use the information on this page for all available agent ids.
This page also shows that your example output indeed belongs to IE 7. More information about the fields in the agent id itself can be found on this page, but as I said already browsers tend to use it for their own benefits and it could be in a (slightly) other format.

How to get exact browser name and version?

Use get_browser()

From Manual:

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

$browser = get_browser(null, true);
print_r($browser);

Will return:

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
[cssversion] => 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] =>
)


Related Topics



Leave a reply



Submit