note 100203 added to function.get-browser

From: Date: Thu, 30 Sep 2010 20:08:54 +0000
Subject: note 100203 added to function.get-browser
Groups: php.notes 
Request: Send a blank email to php-notes+get-172856@lists.php.net to get a copy of this message
Added few lines of code to the Robert's browser_info() function to get the full OS details from the $_SERVER['HTTP_USER_AGENT']. Made changes to the parser to make it work for google chrome and MAC user agents. Currently, this code is good for the user agents listed in http://www.peacemax.com/11/php/user-agents/ <?php /** * * Gets the OS, browser name and browser version of the client * @param $agent */ public static function getClientBrowserInfo($agent=null) { // Declare known browsers to look for $known = array('msie', 'firefox','chrome', 'safari', 'webkit', 'opera', 'netscape', 'konqueror', 'gecko', 'googlebot', 'googlebot-image', 'ask', 'msnbot-products'); $os_known = array('linux','mac','win'); // Clean up agent and build regex that matches phrases for known browsers // (e.g. "Firefox/2.0" or "MSIE 6.0" (This only matches the major and minor // version numbers. E.g. "2.0.0.6" is parsed as simply "2.0" $agent = strtolower($agent ? $agent : $_SERVER['HTTP_USER_AGENT']); // Use the User Agent String to get the OS full details. $os = ''; if(preg_match('#\(.*?(['.join('|',$os_known).'].*?)\)#i', $agent, $os_matches) != 0) { $os = $os_matches[1]; } $pattern = '#(?<browser>' . join('|', $known) . ')[/ ]+(?<version>[0-9]+(?:\.[0-9]+)?)#i'; // Find all phrases (or return empty array if none found) if (!preg_match_all($pattern, $agent, $matches)) return array('name'=>'','version'=>'','os' => $os); //A tweak for chrome in Mac as this is giving safari as the last match. if($key = array_search('chrome', $matches['browser'])) return array('name'=> $matches['browser'][$key], 'version' => $matches['version'][$key], 'os'=>$os ); // Since some UAs have more than one phrase (e.g Firefox has a Gecko phrase, // Opera 7,8 have a MSIE phrase), use the last one found (the right-most one // in the UA). That's usually the most correct. $i = count($matches['browser'])-1; return array('name'=> $matches['browser'][$i], 'version' => $matches['version'][$i], 'os'=>$os); } ?> Hope this will be useful. I have used this for database log of the user details when he registers, logs-in and logs-off. ---- Server IP: 69.147.83.197 Probable Submitter: 115.184.23.189 ---- Manual Page -- http://www.php.net/manual/en/function.get-browser.php Edit -- https://master.php.net/note/edit/100203 Del: integrated -- https://master.php.net/note/delete/100203/integrated Del: useless -- https://master.php.net/note/delete/100203/useless Del: bad code -- https://master.php.net/note/delete/100203/bad+code Del: spam -- https://master.php.net/note/delete/100203/spam Del: non-english -- https://master.php.net/note/delete/100203/non-english Del: in docs -- https://master.php.net/note/delete/100203/in+docs Del: other reasons-- https://master.php.net/note/delete/100203 Reject -- https://master.php.net/note/reject/100203 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#172856) next »