jump to navigation

How To Detect Browsers and Mobile Devices With PHP January 21, 2012

Posted by Tournas Dimitrios in PHP.
trackback

Browsing the Internet with your favorite “client” is based on strict rules (communications protocols) , they are handled automatically  , so you don’t have to worry about them . Probably the most used protocol is the Transmission Control Protocol (TCP) which  is connection-oriented .
Connection-oriented means that , before any data can be transmitted , a reliable connection must be obtained (instantiated) and acknowledged . The instatiation of this connection ( accomplished in 3 steps ) is done  with  “header” messages  which are send between the two nodes (client–server)  . While browser’s headers  notify the server what resources he would like to receive , server’s headers notify the acception of the connection and some other directives  (content-type , caching , redirecting ) . TCP’s three way handshaking technique is referred to as the 3-way handshake or as “SYN-SYN-ACK” and is the method used to establish TCP socket connections and tear down TCP socket connections over the network .

One of client’s (browsers) instantiation  header ,   is the Accept header ,  which takes a list of options ,  tells the server what file formats , or more correctly MIME-types , the browser is looking for . The result of a “ echo $_SERVER[‘HTTP_ACCEPT’] ; ” will return a string with the following signature  :
text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 

To minimize the Back-and-forth communication (during instantiation ) the content negotiation happens in a single  list of options which are indicated by the “relative quality parameter”  (q) and its value (qvalue) — application/xml;q=0.9,*/*;q=0.8 —-

Another header worth to mention is the “$_SERVER[‘HTTP_USER_AGENT’]” , it’s signature is similar to  the following string :
Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.75 Safari/535.7
A quick Browser-type detection could be made as :
echo  strpos($_SERVER[‘HTTP_USER_AGENT’],’Chrome’) ? “Yes” : “No” ;

The hottest device out there right now seems to be the iPad , it’s HTTP_USER_AGENT – signature is : Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10
Detecting IPad’s could be done as :
echo (Bool) stripos($_SERVER[‘HTTP_USER_AGENT’] ,’iPad’) ? “Yes” : “No” ;
Detecting Android could be done as :
echo (Bool) stripos($_SERVER[‘HTTP_USER_AGENT’] ,’android’) ? “Yes” : “No” ; 
Using some logic from Drew Douglass’ excellent mobile redirection post , we can redirect users to a mobile version of a website if we desire to do so  .

I have over-simplified the theory as my intention was to give the basic concepts of Browser detection . We don’t really have to know more about it , as there are excellent open-source PHP Classes developed under the GPL licence . Two of these Classes   are :

  • browser.php : It has a long list of features ,  a basic example how to use this Class is :
    < ?php
    
    require_once('Browser.php');
    $browser = new Browser();
    if( ! ( $browser->getBrowser() == Browser::BROWSER_FIREFOX && $browser->getVersion() >= 2 ) ) {
    echo ‘You have FireFox version 2 or greater’;
    }
    
    ?>
    
  • Mobile_Detect.php : As its name notifies , it detect mobile devices . A basic example how to use this Class is :
    include("Mobile_Detect.php");
    $detect = new Mobile_Detect();
    if ($detect->isAndroid()) {
     // code to run for the Google Android platform
    	}
    
    

Examine HTML headers in Firefox and Google Chrome :

In Firefox just install the firebug plugin , open the page for which you would like to examine HTTP headers and enable  Firebug (Press F12) -> Click on Page Speed , then click on Resources and then on the left, click on the + sign of every HTTP request you would like to examine . You can view the header code without expanding the request .
In Google Chrome , open the page for which you would like to examine the HTTP headers . Press Ctrl + Shift + i, shortcut key for Developers Tool . Click on “Network” tab , from the left-column select on  every HTTP request you would like to examine (header tab) .

Links worth to visit :

Comments»

No comments yet — be the first.

Leave a comment