PHP OS (Operating System) Detection

///PHP OS (Operating System) Detection

PHP OS (Operating System) Detection

Now a days with cross browser compatibility issues ranging outside of just Windows/PC as Mac’s and Linux become more user friendly and popular you get issues that aren’t necessarily the same thing cross OS’s now. Example something I do in chrome on my Windows PC works well, where on my MAC it fails. So what do I do? I spend either a lot of time making it work for both often losing functionality or I make two different versions one working for the need of each OS. How do I detect the OS? easy Ill show you in a second. \r\nThe other reason someone may want to have OS detection is cause they offer software for example. For all platforms. But to make the user experience more friendly they want to give the user the ability to download the software faster, without having to dig through a list of all operating systems for there match. This is a quick way to build a landing page based on OS as well.\r\n ‘windows nt 6.1’,\r\n ‘Windows Vista’ => ‘windows nt 6.0’,\r\n ‘Windows Server 2003’ => ‘windows nt 5.2’,\r\n ‘Windows XP’ => ‘windows nt 5.1’,\r\n ‘Windows 2000 sp1’ => ‘windows nt 5.01’,\r\n ‘Windows 2000’ => ‘windows nt 5.0’,\r\n ‘Windows NT 4.0’ => ‘windows nt 4.0’,\r\n ‘Windows Me’ => ‘win 9x 4.9’,\r\n ‘Windows 98’ => ‘windows 98’,\r\n ‘Windows 95’ => ‘windows 95’,\r\n ‘Windows CE’ => ‘windows ce’,\r\n ‘Windows (version unknown)’ => ‘windows’,\r\n ‘OpenBSD’ => ‘openbsd’,\r\n ‘SunOS’ => ‘sunos’,\r\n ‘Ubuntu’ => ‘ubuntu’,\r\n ‘Linux’ => ‘(linux)|(x11)’,\r\n ‘Mac OSX Beta (Kodiak)’ => ‘mac os x beta’,\r\n ‘Mac OSX Cheetah’ => ‘mac os x 10.0’,\r\n ‘Mac OSX Puma’ => ‘mac os x 10.1’,\r\n ‘Mac OSX Jaguar’ => ‘mac os x 10.2’,\r\n ‘Mac OSX Panther’ => ‘mac os x 10.3’,\r\n ‘Mac OSX Tiger’ => ‘mac os x 10.4’,\r\n ‘Mac OSX Leopard’ => ‘mac os x 10.5’,\r\n ‘Mac OSX Snow Leopard’ => ‘mac os x 10.6’,\r\n ‘Mac OSX Lion’ => ‘mac os x 10.7’,\r\n ‘Mac OSX (version unknown)’ => ‘mac os x’,\r\n ‘Mac OS (classic)’ => ‘(mac_powerpc)|(macintosh)’,\r\n ‘QNX’ => ‘QNX’,\r\n ‘BeOS’ => ‘beos’,\r\n ‘OS2’ => ‘os/2’,\r\n ‘SearchBot’=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)’\r\n );\r\n \r\n $useragent = $_SERVER[‘HTTP_USER_AGENT’];\r\n $useragent = strtolower($useragent);\r\n \r\n foreach($osList as $os=>$match) {\r\n if (preg_match(‘/’ . $match . ‘/i’, $useragent)) {\r\n break; \r\n }\r\n else\r\n {\r\n //$os = “Not automatically detected.
$useragent”; \r\n $os = “unknown”;\r\n }\r\n }\r\n \r\n return $os;\r\n }\r\n?>\r\nExample: Your OS: \r\n\r\n
\r\n	function getUserOS(){\r\n	$osList = array\r\n	(\r\n		'Windows 7' => 'windows nt 6.1',\r\n		'Windows Vista' => 'windows nt 6.0',\r\n		'Windows Server 2003' => 'windows nt 5.2',\r\n		'Windows XP' => 'windows nt 5.1',\r\n		'Windows 2000 sp1' => 'windows nt 5.01',\r\n		'Windows 2000' => 'windows nt 5.0',\r\n		'Windows NT 4.0' => 'windows nt 4.0',\r\n		'Windows Me' => 'win 9x 4.9',\r\n		'Windows 98' => 'windows 98',\r\n		'Windows 95' => 'windows 95',\r\n		'Windows CE' => 'windows ce',\r\n		'Windows (version unknown)' => 'windows',\r\n		'OpenBSD' => 'openbsd',\r\n		'SunOS' => 'sunos',\r\n		'Ubuntu' => 'ubuntu',\r\n		'Linux' => '(linux)|(x11)',\r\n		'Mac OSX Beta (Kodiak)' => 'mac os x beta',\r\n		'Mac OSX Cheetah' => 'mac os x 10.0',\r\n		'Mac OSX Puma' => 'mac os x 10.1',\r\n		'Mac OSX Jaguar' => 'mac os x 10.2',\r\n		'Mac OSX Panther' => 'mac os x 10.3',\r\n		'Mac OSX Tiger' => 'mac os x 10.4',\r\n		'Mac OSX Leopard' => 'mac os x 10.5',\r\n		'Mac OSX Snow Leopard' => 'mac os x 10.6',\r\n		'Mac OSX Lion' => 'mac os x 10.7',\r\n		'Mac OSX (version unknown)' => 'mac os x',\r\n		'Mac OS (classic)' => '(mac_powerpc)|(macintosh)',\r\n		'QNX' => 'QNX',\r\n		'BeOS' => 'beos',\r\n		'OS2' => 'os/2',\r\n		'SearchBot'=>'(nuhk)|(googlebot)|(yammybot)|(openbot)|(slurp)|(msnbot)|(ask jeeves/teoma)|(ia_archiver)'\r\n	);\r\n	\r\n	$useragent = $_SERVER['HTTP_USER_AGENT'];\r\n	$useragent = strtolower($useragent);\r\n	\r\n	foreach($osList as $os=>$match) {\r\n		if (preg_match('/' . $match . '/i', $useragent)) {\r\n			break;	\r\n		}\r\n		else\r\n		{\r\n			//$os = "Not automatically detected.
$useragent"; \r\n $os = "unknown";\r\n }\r\n }\r\n \r\n return $os;\r\n }\r\n
By |2014-01-05T10:29:06+00:00January 27th, 2012|Code, PHP|0 Comments

About the Author:

Not much to know about me, I'm a 35+ year old coder, geek, gamer..

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: