Current Location: Home> Latest Articles> How to use get_client_version to detect client browser version

How to use get_client_version to detect client browser version

gitbox 2025-05-06

In PHP, detecting client browser versions is a common requirement. Especially for web applications that require different functions or styles to different browser versions, version control is very important. We can achieve this by getting the User-Agent string of the client. This article will use the custom get_client_version function to detect the client browser version and perform appropriate control based on the version information.

1. Get the User-Agent of the client browser

Each time the client requests, the browser will send an HTTP header named User-Agent , which contains the browser type, version, operating system and other information. PHP provides $_SERVER['HTTP_USER_AGENT'] to obtain this information.

We first define a function to extract the browser type and version.

 function get_client_version() {
    // Get User-Agent String
    $userAgent = $_SERVER['HTTP_USER_AGENT'];

    // Browser version
    $browserVersion = '';
    // Detect different browsers
    if (preg_match('/MSIE (.*?);/', $userAgent, $matches)) {
        $browserVersion = "Internet Explorer: " . $matches[1];
    } elseif (preg_match('/Trident\/.*rv:(.*?)\)/', $userAgent, $matches)) {
        $browserVersion = "Internet Explorer: " . $matches[1];
    } elseif (preg_match('/Chrome\/(.*?) /', $userAgent, $matches)) {
        $browserVersion = "Chrome: " . $matches[1];
    } elseif (preg_match('/Firefox\/(.*?) /', $userAgent, $matches)) {
        $browserVersion = "Firefox: " . $matches[1];
    } elseif (preg_match('/Safari\/(.*?) /', $userAgent, $matches)) {
        $browserVersion = "Safari: " . $matches[1];
    } elseif (preg_match('/Edge\/(.*?) /', $userAgent, $matches)) {
        $browserVersion = "Edge: " . $matches[1];
    } else {
        $browserVersion = "Unknown Browser";
    }

    return $browserVersion;
}

In the above code, we use regular expressions to extract browser type and version information in the User-Agent string. Different browsers have different identifiers, for example, Chrome's version number is usually behind Chrome/ , while Internet Explorer uses MSIE or Trident .

2. Perform version control

Once we extract the browser version information, we can then control it differently according to the version. Suppose we want to do some compatibility with some old versions of browsers, or enable some advanced features for new versions of browsers, we can use version information to branch control.

For example, we can check the Chrome version and provide different features according to the version:

 function version_control() {
    $browserVersion = get_client_version();
    
    // GetBrowser名称和Version号
    preg_match('/([a-zA-Z]+): (\d+\.\d+)/', $browserVersion, $matches);
    
    $browserName = $matches[1] ?? 'Unknown';
    $version = $matches[2] ?? '0';

    // right Chrome Perform version control
    if ($browserName == 'Chrome') {
        if (floatval($version) < 90) {
            echo "Yours Chrome Browser version过低,请升级以Get更好的体验。";
        } else {
            echo "Yours Chrome Browser version支持最新功能!";
        }
    } elseif ($browserName == 'Firefox') {
        // right Firefox Perform similar version control
        if (floatval($version) < 80) {
            echo "Yours Firefox Browser version过低,请升级以Get更好的体验。";
        } else {
            echo "Yours Firefox Browser version支持最新功能!";
        }
    } else {
        echo "Detected that you are using $browserName Browser,Version: $version。";
    }
}

In this version_control function, we first call get_client_version to get the browser version information. Next, we perform different logical processing based on the name and version of the browser. For example, if the version of Chrome is lower than 90, the user will be reminded to upgrade the browser.

3. Complete sample code

Combining the previous code, we have a complete example that can be used in the PHP page:

 <?php
function get_client_version() {
    $userAgent = $_SERVER['HTTP_USER_AGENT'];
    $browserVersion = '';
    
    if (preg_match('/MSIE (.*?);/', $userAgent, $matches)) {
        $browserVersion = "Internet Explorer: " . $matches[1];
    } elseif (preg_match('/Trident\/.*rv:(.*?)\)/', $userAgent, $matches)) {
        $browserVersion = "Internet Explorer: " . $matches[1];
    } elseif (preg_match('/Chrome\/(.*?) /', $userAgent, $matches)) {
        $browserVersion = "Chrome: " . $matches[1];
    } elseif (preg_match('/Firefox\/(.*?) /', $userAgent, $matches)) {
        $browserVersion = "Firefox: " . $matches[1];
    } elseif (preg_match('/Safari\/(.*?) /', $userAgent, $matches)) {
        $browserVersion = "Safari: " . $matches[1];
    } elseif (preg_match('/Edge\/(.*?) /', $userAgent, $matches)) {
        $browserVersion = "Edge: " . $matches[1];
    } else {
        $browserVersion = "Unknown Browser";
    }

    return $browserVersion;
}

function version_control() {
    $browserVersion = get_client_version();
    preg_match('/([a-zA-Z]+): (\d+\.\d+)/', $browserVersion, $matches);
    
    $browserName = $matches[1] ?? 'Unknown';
    $version = $matches[2] ?? '0';

    if ($browserName == 'Chrome') {
        if (floatval($version) < 90) {
            echo "Yours Chrome Browser version过低,请升级以Get更好的体验。";
        } else {
            echo "Yours Chrome Browser version支持最新功能!";
        }
    } elseif ($browserName == 'Firefox') {
        if (floatval($version) < 80) {
            echo "Yours Firefox Browser version过低,请升级以Get更好的体验。";
        } else {
            echo "Yours Firefox Browser version支持最新功能!";
        }
    } else {
        echo "Detected that you are using $browserName Browser,Version: $version。";
    }
}

// 调用Version控制
version_control();
?>

The above code can help you provide different user experiences or reminders based on different browser versions. You can further expand according to your needs, such as adding more browser support, or enabling specific front-end features in certain versions.