matthew
7/27/2014 - 3:23 PM

PHP - detect browser.php

<?php
/**
 * getBrowser
 *
 * @param void
 * @return string
 */
function getBrowser()
{
    $browser = array(
        'version'   => '0.0.0',
        'majorver'  => 0,
        'minorver'  => 0,
        'build'     => 0,
        'name'      => 'unknown',
        'useragent' => ''
    );

    $browsers = array(
        'firefox', 'msie', 'opera', 'chrome', 'safari', 'mozilla', 'seamonkey', 'konqueror', 'netscape',
        'gecko', 'navigator', 'mosaic', 'lynx', 'amaya', 'omniweb', 'avant', 'camino', 'flock', 'aol'
    );

    if ($_SERVER['HTTP_USER_AGENT']) {
        $browser['useragent'] = $_SERVER['HTTP_USER_AGENT'];
        $userAgent = strtolower($browser['useragent']);
        foreach($browsers as $_browser) {
            if (preg_match("/($_browser)[\/ ]?([0-9.]*)/", $userAgent, $match)) {
                $browser['name'] = $match[1];
                $browser['version'] = $match[2];
                @list($browser['majorver'], $browser['minorver'], $browser['build']) = explode('.', $browser['version']);
                break;
            }
        }
    }
    return $browser['name'].' '.$browser['version'];
}

echo getBrowser();