2015年2月23日 星期一

Browser Detection With PHP Browscap: Have a Better Solution Than GET_BROWSER()

Basically, long time ago, browscap.ini and browscap.dll were created by Microsoft as an easy way to get information about the user’s browser.
Browser detection these days is a mess! With over 100,000 known browser userAgents we need an easier way of getting info about the user’s browser without checking all of the known userAgents manually.
PHP has a built in function for using browscap.ini; but unfortunately, that function doesn’t always work fine because:
a) most servers either don’t have a browscap.ini file or have an outdated version
b) get_browser(); really slows down your application, architecture of parsing, caching, execution time, etc.
c) binding and setting of “browscap” directive in php.ini on shared-hosting systems and keeping the databaseup to date
Code example get_browser();
< ?php
echo $_SERVER['HTTP_USER_AGENT'] . "\n\n";
$browser = get_browser(null, true);
print_r($browser);
?>
Today we can find many different, standalone and of course improved classes of Browser Detection in PHP, so i want to introduce another one here:
phpbrowscap – Browscap PHP Project
Browscap is a standalone class for both PHP4 and PHP5 that gets around the limitations of get_browser()and manages the whole thing. It offers methods to update, cache, adapt and get details about every supplied user agent on a standalone basis.
Code example of Browscap:
< ?php
require('Browscap.php');
$bc = new Browscap('cache');
$bc->localFile = 'php_browscap.ini';
$data = $bc->getBrowser();
?>

to update your browscap.ini file automatically, which is also necessary here, have a look atphcomp.co.uk/Packages/UpdateBrowsCap.html or use a cURL cronjob to get a new ini-file.

沒有留言:

張貼留言