Current Location: Home> Latest Articles> Use get_client_version to obtain user operating system information

Use get_client_version to obtain user operating system information

gitbox 2025-05-06

In web development, obtaining the operating system information of the client is a common requirement, especially when it is necessary to optimize the user experience based on different operating systems. PHP is a popular server-side programming language, but it does not directly provide built-in functions to obtain client operating system information. However, there are some ways we can obtain this kind of information. In this article, we will implement this function through the custom get_client_version function.

What is the get_client_version function?

get_client_version is a PHP function we customized to extract operating system information from the user's browser request. Through this function, we can obtain the user's operating system version and then make some conditional judgments to optimize the responsive design or specific functions of the website.

How to get operating system information through the get_client_version function?

The following is the code that implements the get_client_version function, which parses the browser's User-Agent string to determine the user's operating system.

 <?php
function get_client_version() {
    // Get the browser sent User-Agent String
    $user_agent = $_SERVER['HTTP_USER_AGENT'];
    
    // Define regular expressions that match the operating system
    $os_patterns = array(
        'Windows' => '/Windows NT/i',
        'Mac' => '/Macintosh/i',
        'Linux' => '/Linux/i',
        'Android' => '/Android/i',
        'iOS' => '/iPhone|iPad|iPod/i'
    );
    
    // Iterate through the regular expressions of each operating system,Check if it matches
    foreach ($os_patterns as $os => $pattern) {
        if (preg_match($pattern, $user_agent)) {
            return $os;
        }
    }
    
    return 'Unknown OS'; // If no OS is matched,Return to unknown
}

// Example:Get and output the client&#39;s operating system information
$client_os = get_client_version();
echo "Client OS: " . $client_os;
?>

Code parsing

  1. Get the user's User-Agent
    Use $_SERVER['HTTP_USER_AGENT'] to get the User-Agent string in the browser request header. This string contains browser, operating system, device information, etc.

  2. Define the matching pattern of the operating system <br> In the get_client_version function, we use regular expressions to match common operating systems such as Windows, Mac, Linux, Android, and iOS. These regular expressions can be matched according to the identification of different operating systems.

  3. Regular Match <br> Use the preg_match() function to match the User-Agent string. If a regular expression matches the User-Agent string successfully, the corresponding operating system name is returned.

  4. Return to operating system information <br> If no operating system information is matched, the default return of 'Unknown OS' is returned.

Things to note

  • User-Agent strings are not fixed, and different browsers and devices may have different formats. Therefore, matching of certain operating systems may require further optimization.

  • Some modern browsers may hide or modify the User-Agent string, so User-Agent -based detection may not be completely reliable.

  • If you want to provide different services or functions according to the operating system, you can combine the get_client_version function for further judgment and processing.

Processing of replacing URLs

In actual development, we often need to obtain some information or data from external resources. In the code we provide, if you need to access external resources or APIs, you can use a domain name such as gitbox.net . You can use the following code to replace the original URL:

 $url = 'https://gitbox.net/api/getClientOS';
$response = file_get_contents($url);

In this way, you can flexibly replace it with other domain names to ensure that it matches the actual business needs.