Detect operating system from user agent string

Detect operating system from user agent string
This code uses an array of user strings and their matching operating systems so that it can detect what operatings system the user is using.
$OSList = array(
    'Windows 3.11' => 'Win16',
    'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
    'Windows 98' => '(Windows 98)|(Win98)',
    'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
    'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
    'Windows Server 2003' => '(Windows NT 5.2)',
    'Windows Vista' => '(Windows NT 6.0)',
    'Windows 7' => '(Windows NT 7.0)',
    'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
    'Windows ME' => 'Windows ME',
    'Open BSD' => 'OpenBSD',
    'Sun OS' => 'SunOS',
    'Linux' => '(Linux)|(X11)',
    'Mac OS' => '(Mac_PowerPC)|(Macintosh)',
    'QNX' => 'QNX',
    'BeOS' => 'BeOS',
    'OS/2' => 'OS/2',
    'Search Bot' => '(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);

foreach ($OSList as $CurrOS => $Match) {
    if (eregi($Match, $_SERVER['HTTP_USER_AGENT'])) {
        break;
    }
}

echo "You are using " . $CurrOS;
  • The script starts by defining an associative array $OSList where each key is the name of an operating system (or other user agent type like ‘Search Bot’), and each value is a regular expression pattern matching the user agent strings that correspond to that OS.
  • The foreach loop iterates through each key-value pair in $OSList. $CurrOS is the name of the current OS being checked, and $Match is its associated regex pattern.
  • eregi($Match, $_SERVER['HTTP_USER_AGENT']) checks if the current user’s agent string (from $_SERVER['HTTP_USER_AGENT']) matches the regex pattern $Match. If it does, the loop breaks, meaning $CurrOS holds the name of the detected OS.
  • Finally, the script echoes a message stating the OS detected. Note: This will echo the last OS in the list if no match is found, which might not be accurate.

It should be noted that the eregi function used in the script is deprecated as of PHP 5.3.0 and removed as of PHP 7.0.0. For modern PHP versions, preg_match with a properly formatted pattern should be used instead.

Here’s a revised version of the code that’s compatible with PHP 7.0.0 and higher. This version uses preg_match, which is the recommended function for regular expression matching in PHP 7:

$OSList = array(
    'Windows 3.11' => 'Win16',
    'Windows 95' => '(Windows 95|Win95|Windows_95)',
    'Windows 98' => '(Windows 98|Win98)',
    'Windows 2000' => '(Windows NT 5.0|Windows 2000)',
    'Windows XP' => '(Windows NT 5.1|Windows XP)',
    'Windows Server 2003' => 'Windows NT 5.2',
    'Windows Vista' => 'Windows NT 6.0',
    'Windows 7' => 'Windows NT 7.0',
    'Windows NT 4.0' => '(Windows NT 4.0|WinNT4.0|WinNT|Windows NT)',
    'Windows ME' => 'Windows ME',
    'Open BSD' => 'OpenBSD',
    'Sun OS' => 'SunOS',
    'Linux' => '(Linux|X11)',
    'Mac OS' => '(Mac_PowerPC|Macintosh)',
    'QNX' => 'QNX',
    'BeOS' => 'BeOS',
    'OS/2' => 'OS/2',
    'Search Bot' => '(nuhk|Googlebot|Yammybot|Openbot|Slurp|MSNBot|Ask Jeeves/Teoma|ia_archiver)'
);

$detectedOS = 'Unknown OS';
foreach ($OSList as $CurrOS => $Match) {
    if (preg_match("/$Match/", $_SERVER['HTTP_USER_AGENT'])) {
        $detectedOS = $CurrOS;
        break;
    }
}

echo "You are using " . $detectedOS;

Changes Made:

  1. Regular Expression Delimiters: Added delimiters (/) around the regular expression patterns in preg_match.
  2. Default OS: Set a default OS (Unknown OS) to handle cases where no OS matches.

This updated script is now compatible with PHP 7.0.0 and later versions, properly using preg_match for regular expression matching.

PHP code that includes both the old and new operating systems for detecting the OS from a user agent string:

$OSList = array(
    // Old OS entries
    'Windows 3.11' => 'Win16',
    'Windows 95' => '(Windows 95|Win95|Windows_95)',
    'Windows 98' => '(Windows 98|Win98)',
    'Windows 2000' => '(Windows NT 5.0|Windows 2000)',
    'Windows XP' => '(Windows NT 5.1|Windows XP)',
    'Windows Server 2003' => 'Windows NT 5.2',
    'Windows Vista' => 'Windows NT 6.0',
    'Windows 7' => 'Windows NT 7.0',
    'Windows NT 4.0' => '(Windows NT 4.0|WinNT4.0|WinNT|Windows NT)',
    'Windows ME' => 'Windows ME',
    'Open BSD' => 'OpenBSD',
    'Sun OS' => 'SunOS',
    'Linux' => '(Linux|X11)',
    'Mac OS' => '(Mac_PowerPC|Macintosh)',
    'QNX' => 'QNX',
    'BeOS' => 'BeOS',
    'OS/2' => 'OS/2',
    'Search Bot' => '(nuhk|Googlebot|Yammybot|Openbot|Slurp|MSNBot|Ask Jeeves/Teoma|ia_archiver)',

    // New OS entries
    'Windows 11' => '(Windows NT 10.0)', // For Windows 11
    'Android OS 12' => '(Android 12)', // For Android OS 12
    'macOS Monterey' => '(Macintosh.*macOS 12)', // For macOS 12 Monterey
    'Ubuntu 20.04 LTS' => '(Ubuntu 20.04)', // For Ubuntu 20.04 LTS
    'Solaris 11.4' => '(Solaris 11.4)', // For Solaris 11.4
    'Cisco NX-OS' => '(Cisco NX-OS)', // For Cisco NX-OS
    'Red Hat Enterprise Linux 20.04' => '(RHEL 20.04)|(Red Hat Enterprise Linux)', // For RHEL 20.04
    'SteamOS 2.195' => '(SteamOS)', // For SteamOS 2.195
    // You can continue adding more entries as needed
);

// Rest of the code for user-agent detection...

This code includes the previous operating systems and adds the new ones as discussed. Remember to test this code thoroughly, as user agent strings can vary and might not always match these patterns exactly. Additionally, keep in mind that some operating systems, especially newer ones, may receive updates that change their user agent strings, so it’s important to keep your detection logic updated.

Nathan Pakovskie is an esteemed senior developer and educator in the tech community, best known for his contributions to Geekpedia.com. With a passion for coding and a knack for simplifying complex tech concepts, Nathan has authored several popular tutorials on C# programming, ranging from basic operations to advanced coding techniques. His articles, often characterized by clarity and precision, serve as invaluable resources for both novice and experienced programmers. Beyond his technical expertise, Nathan is an advocate for continuous learning and enjoys exploring emerging technologies in AI and software development. When he’s not coding or writing, Nathan engages in mentoring upcoming developers, emphasizing the importance of both technical skills and creative problem-solving in the ever-evolving world of technology. Specialties: C# Programming, Technical Writing, Software Development, AI Technologies, Educational Outreach

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top