Get operating system info

Get operating system info

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Parsing User Agents for Operating System Detection

Sites like thismachine.info display detailed client fingerprints, including the operating system running on the user's device. While the browser identification is straightforward, extracting the underlying OS requires a different approach than simply querying an API or checking server logs alone. The primary mechanism relies entirely on the User-Agent string embedded in every HTTP request.

The User-Agent header acts as a self-identification tag for the client. When a user opens a browser, it sends this string to the server before any data exchange occurs. For example, a request from a Windows 10 machine might include a string resembling Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36.... The crucial segment here is (Windows NT 10.0), which explicitly declares the OS version. Similarly, macOS clients often return Macintosh; Intel Mac OS X 10_15_7, while Linux users might see X11; Linux x86_64.

To replicate the functionality of these detection sites in PHP, you must parse this header string programmatically. Modern PHP environments do not include a built-in function to decode these strings reliably because the format has evolved over decades and varies significantly between vendors. Relying on native regex patterns often leads to brittle code that breaks with minor updates to browser or OS naming conventions.

The most robust solution is to integrate a specialized library designed specifically for this parsing task. Libraries like pecl/http-useragent or dedicated PHP packages maintain up-to-date mappings of known User-Agent strings to their corresponding operating systems and devices. For instance, a package might map the substring Android combined with version numbers to the Android OS, distinguishing it from ChromeOS or Windows Phone variants which can share similar UA prefixes.

use Http\Parser\UserAgentParser;

$parser = new UserAgentParser();
$agent = $_SERVER['HTTP_USER_AGENT']; // e.g., "Mozilla/5.0 (Windows NT 10.0; Win64; x64) ..."

try {
    $parsed = $parser->parse($agent);
    
    if ($parsed && isset($parsed->os)) {
        echo "Detected OS: " . $parsed->os->family; // Outputs: Windows
        echo "Version: " . ($parsed->os->major ?? 'Unknown'); // Outputs: 10
    } else {
        echo "Operating system could not be determined.";
    }
} catch (\Exception $e) {
    // Handle parsing errors gracefully
    echo "Error parsing User-Agent.";
}

Using a parser abstracts away the complexity of maintaining regex rules. When an OS vendor changes their identifier—say, renaming a kernel version or introducing a new mobile platform—the library maintainers update the mapping database, and your code continues to work without modification. This is a critical trade-off: while it introduces a dependency, it saves significant development time and reduces the risk of false positives that plague custom implementations.

However, developers must remain aware of the limitations inherent in this method. The User-Agent header can be spoofed easily. A malicious actor or a privacy-focused user can configure their browser to send a generic or fabricated string, effectively hiding their true operating system. Furthermore, some modern browsers and enterprise security software modify these headers to comply with privacy standards or corporate policies, potentially stripping out specific OS identifiers entirely.

If you require higher accuracy for critical authentication flows or device-specific routing, relying solely on the User-Agent is insufficient. In such cases, you should combine header analysis with other signals, such as the TLS fingerprint provided by libraries like fingerprintjs, which analyzes canvas rendering and audio processing capabilities to infer hardware and OS characteristics more securely.

For Laravel projects specifically, integrating these parsing tools can be done seamlessly within your service classes or middleware. If you are building a platform that needs to adapt its interface based on the client's environment, leveraging established libraries ensures consistency across your user base without reinventing the wheel. Resources like https://laravelcompany.com often discuss best practices for handling client-side data in modern web applications, emphasizing the balance between user convenience and technical accuracy.

Our network