Archive

Posts Tagged ‘agent’

The Google Chrome User Agent

September 3rd, 2008 No comments

As the new Google web browser was released last night (I’m writing this post using the new browser) I thought it would be good to update our readers on the user agent string that this web browser has.

The user agent of any browser can be found out by using the userAgent property of the navigator object. This is available in most modern browsers and is thankfully also present in Google Chrome.

navigator.userAgent

As an example the user agent for FireFox 3 on a Windows XP machine looks like this.

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1

Using the same code, and the same machine, the user agent produced by Google Chrome is as follows.

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.27 Safari/525.13

So detecting it should only be a case of looking for the word “chrome”. Just like this:

var ischrome = navigator.userAgent.indexOf("Chrome")? true : false;

If you want to see what the user agent is on your machine then past this code into a web page and hit refresh. It is quite a basic bit of code and should work in most browsers.

<span id="useragent"></span>
<script type="text/javascript">
document.getElementById('useragent').innerHTML = navigator.userAgent;
</script>

PHP Browser Detection

March 27th, 2008 No comments

Retrieving the current user agent using PHP is done via the use of the $_SESSION super global array. The following line of code will print off your user agent.

echo $_SERVER['HTTP_USER_AGENT'];

For Firefox on Windows this user agent will look like this.

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12

This is all fine, but what about getting more meaningful information, like just the version number. Here is a function that will get the version number (major or minor) from anyone visiting a page with Internet Explorer.

function ieVersion($minor=false){
 preg_match('/MSIE ([0-9]\.[0-9])/',$_SERVER['HTTP_USER_AGENT'],$match);
 if(!isset($match[1])){
  return -1;
 }else{
  if($minor){
   return $match[1];
  }else{
   return floor($match[1]);
  }
 }
}

This will work for all of the different versions of Internet Explorer, but what about other browsers. One solution might be to create lots of functions that each checked the user agent string for a specific string. However, this is just overcomplicating things. Here is a function that will detect the user agent and version number and present it in a nice form.

function userAgent($minor=false){
 $agents = array('MSIE'=>'/MSIE ([0-9]\.[0-9])/',
  'Firefox'=>'/Firefox\/([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*)/',
  'Opera'=>'/Opera\/([0-9]*\.[0-9])/',
  'Safari'=>'/Version\/([0-9]*\.[0-9]*\.?[0-9]*)/');
 foreach($agents as $agent=>$pattern){
  preg_match($pattern,$_SERVER['HTTP_USER_AGENT'],$match);
  if(isset($match[1])){
   if($minor){
    return $agent.' '.$match[1];
   }else{
    return $agent.' '.floor($match[1]);
   };
  };
 };
 return 'Unknown browser';
}

This only checks for four different browser types so if you want to add more then just append the name and pattern to match to the array. To run this function and find out the browser version of a visitor use the following code.

echo userAgent(true).'<br />';

From the user agent example given above this will output Firefox 2.0.0.12.

This is a very simplified mechanism of fetching user agents as it just gives a version number. To get a very clear idea about the capabilities of the browser you can use the get_browser() function. By passing a user agent string into the function it will return an array detailing everything that the browser is capable of, right down to whether cookies or JavaScript is enabled. The only problem is that a browser.ini file is needed to decode the user agent string, and this is not always available.

Categories: PHP Tags: , , , , , , ,