PHP: Detect the user’s operating system.

Posted on March 9, 2012 by Jimmy K. in Tutorials.

I wrote this function as a companion to my jDetectBrowserType() function. I rarely need to use it, but it’s good to have in your arsenal just in case. This function will return the user’s operating system as a string and supports the most popular desktop and mobile operating systems.

/* detect the user's operating system. */
function jDetectOperatingSystem($sInput = "") {

if (empty($sInput)) $sInput = $_SERVER['HTTP_USER_AGENT'];
$sInput = strtoupper($sInput);

$aOperatingSystems = array("ANDROID" => "Android",
"BEOS" => "BeOS",
"BLACKBERRY" => "BlackBerry",
"IPAD" => "iPad",
"IPHONE" => "iPhone",
"LINUX" => "Linux",
"MAC" => "Mac OS",
"OPENBSD" => "OpenBSD",
"OS/2" => "OS/2",
"PALM" => "Palm",
"QNX" => "QNX",
"SUNOS" => "Sun OS",
"WIN" => "Windows",
);

foreach ($aOperatingSystems as $oKey => $oValue) {
if (strpos($sInput, $oKey) !== false) return $oValue; // we found a match..
}

return "";

}

An example of this function can be found here.

This function is part of a larger class I wrote called QTils. The latest version of the QTils class can be downloaded from the END[SEVEN] Cloud.

Tags: , ,

 

Jimmy K. is a Chicago-based web developer who actively posts tutorials, articles and insights on his web development blog to help other programmers and developers.

You can find Jimmy on Google+ and Twitter.

 
 
 
 
 

If you like this, please leave a comment.

Name (required)
Email Address (required)
Website
Comments: