eTechStack | Computer & Technology Guide

Detect browser and version in PHP

How to detect browser in PHP? Here is a PHP code to detect browser, browser's version and platform.

  

function browserInfo($ua) 
{ 
    $userAgent = $ua; 
    $bname = 'Unknown';
    $ub=0;
    $platform = 'Unknown';
    $version= "";

    //Check Platform
    if (preg_match('/linux/i', $userAgent)) {
        $platform = 'linux';
    }
    elseif (preg_match('/macintosh|mac os x/i', $userAgent)) {
        $platform = 'mac';
    }
    elseif (preg_match('/windows|win32/i', $userAgent)) {
        $platform = 'windows';
    }
    
    // Matching Browser
    if(preg_match('/MSIE/i',$userAgent) && !preg_match('/Opera/i',$userAgent)) 
    { 
        $bname = 'Internet Explorer'; 
        $ub = "MSIE"; 
    } 
    elseif(preg_match('/Firefox/i',$userAgent)) 
    { 
        $bname = 'Mozilla Firefox'; 
        $ub = "Firefox"; 
    } 
    elseif(preg_match('/Chrome/i',$userAgent)) 
    { 
        $bname = 'Google Chrome'; 
        $ub = "Chrome"; 
    } 
    elseif(preg_match('/Safari/i',$userAgent)) 
    { 
        $bname = 'Apple Safari'; 
        $ub = "Safari"; 
    } 
    elseif(preg_match('/Opera/i',$userAgent)) 
    { 
        $bname = 'Opera'; 
        $ub = "Opera"; 
    } 
    elseif(preg_match('/Netscape/i',$userAgent)) 
    { 
        $bname = 'Netscape'; 
        $ub = "Netscape"; 
    } 
    
    // get version 
    $known = array('Version', $ub, 'other');
    $pattern = '#(?<browser>' . join('|', $known) .
    ')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
    if (!preg_match_all($pattern, $userAgent, $matches)) {
        // no matching 
    }
    
    $i = count($matches['browser']);
    if ($i != 1) {
        if (strripos($userAgent,"Version") < strripos($userAgent,$ub)){
            @$version= $matches['version'][0];
        }
        else {
            @$version= $matches['version'][1];
        }
    }
    else {
        @$version= $matches['version'][0];
    }
    
    if ($version==null || $version=="") {$version="?";}
    
    return array(
        'bname'      => $bname,
        'version'   => $version,
        'platform'  => $platform
        
    );
}

Example:

$ua='Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.71 Safari/537.36';
$info=browserInfo($ua);
echo 'browser: '.$info['bname'].'<br>';
echo 'version: '.$info['version'].'<br>';
echo 'platform: '.$info['platform'];

Output:

Output of above code is: 
browser: Google Chrome
version: 46.0.2490.71
platform: mac 

Detect browser name in PHP

In above code 'bname' will give name of the browser.

Detect browser Version in PHP

'version' will return full version of the browser.

Detect platform in PHP

'platform' will return name of the platform (Windows,Linux or Mac)



23 comments:

  1. Another excellent information,thanks for sharing that valuable information.Really very nice blog information,do I post something on my blog to post something, or wait to post something worth saying.
    java training

    ReplyDelete
  2. This is really a great post. Thank you for taking time to provide us some of the useful and exclusive information with us. Keep on blogging!!

    PHP Training in Chennai

    ReplyDelete
  3. Wonderful article, very useful and well explanation. Your post is extremely incredible. I will refer this to my candidates...
    Dotnet Training in Chennai

    ReplyDelete
  4. Great blog..You have clearly explained about the concept..Step by step explanation is too good to understand..Its very useful for me to understand..Keep on sharing..
    Java Training in Chennai

    ReplyDelete
  5. thanks for sharing wonderful blog with us. its really more helpful to our institute candidates to get aware some useful knowledge keep sharing more information.
    Dot Net training in Chennai

    ReplyDelete
  6. That is very interesting; you are a very skilled blogger. I have shared your website in my social networks!


    SAP training in Chennai

    ReplyDelete
  7. Great articles, first of all Thanks for writing such lovely Post! Earlier I thought that posts are the only most important thing on any blog. But here at Shoutmeloud I found how important other elements are for your blog.Keep update more posts..

    Digital marketing company in Chennai

    ReplyDelete
  8. Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing.
    Regards,

    Core JAVA Training in Chennai | JAVA Training in Chennai

    ReplyDelete
  9. Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing.
    Regards,

    PHP Training in Chennai | Webdesigning Training in Chennai

    ReplyDelete
  10. Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing.
    Regards,
    SAP Training in Chennai with placement | java training in chennai with placement

    ReplyDelete

Powered by Blogger.