Yogesh Chauhan's Blog

How to get user’s Browser and Operating Systems information using PHP?

in PHP on March 26, 2021

Here’s the function we can use to do so.

function getBrowser()
    $u_agent = $_SERVER['HTTP_USER_AGENT'];
    $bname = 'Unknown';
    $platform = 'Unknown';
    $version= "";

    //First get the platform?
    if (preg_match('/linux/i', $u_agent)) {
        $platform = 'linux';
    elseif (preg_match('/macintosh|mac os x/i', $u_agent)) {
        $platform = 'mac';
    elseif (preg_match('/windows|win32/i', $u_agent)) {
        $platform = 'windows';
    // Next get the name of the useragent yes seperately and for good reason
    if(preg_match('/MSIE/i',$u_agent) &#038;&#038; !preg_match('/Opera/i',$u_agent))
        $bname = 'Internet Explorer';
        $ub = "MSIE";
        $bname = 'Mozilla Firefox';
        $ub = "Firefox";
        $bname = 'Google Chrome';
        $ub = "Chrome";
        $bname = 'Apple Safari';
        $ub = "Safari";
        $bname = 'Opera';
        $ub = "Opera";
        $bname = 'Netscape';
        $ub = "Netscape";
    // finally get the correct version number
    $known = array('Version', $ub, 'other');
    $pattern = '#(?<browser>' . join('|', $known) .
    ')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
    if (!preg_match_all($pattern, $u_agent, $matches)) {
        // we have no matching number just continue
    // see how many we have
    $i = count($matches['browser']);
    if ($i != 1) {
        //we will have two since we are not using 'other' argument yet
        //see if version is before or after the name
        if (strripos($u_agent,"Version") < strripos($u_agent,$ub)){
            $version= $matches['version'][0];
        else {
            $version= $matches['version'][1];
    else {
        $version= $matches['version'][0];
    // check if we have a number
    if ($version==null || $version=="") {$version="?";}
    return array(
        'userAgent' => $u_agent,
        'name'      => $bname,
        'version'   => $version,
        'platform'  => $platform,
        'pattern'    => $pattern

// now try it
$yourbrowser= "Your browser: " . $ua['name'] . " " . $ua['version'] . " on " .$ua['platform'] . " reports: <br >" . $ua['userAgent'];

Credit goes to this comment.

Most Read

#1 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
How to compress images with gulp in WordPress?WordPressHow to use [] (square brackets) function in Envision Basic?Envision BasicHow to reference an aliased column in the WHERE clause?SQL/MySQLNew to  Drupal? Here’s What You Need to KnowDrupalSanitize inputs using built-in WordPress functionsWordPressHTML Copyright SymbolHTMLHow to use $INSERT command in Envision Basic?Envision BasicWhat happened to your Google Analytics Tracking ID, can’t find it?MiscellaneousAND, OR and NOT boolean operators in Envision BasicEnvision BasicA list of wp-cli commands to use via SSHWordPressHow to define variables in SCSS (Sass)?SCSSHow to compile and watch Sass using Gulp in WordPress?SCSS