OS Detection

JavaScript

To detect the OS, you can use the following JavaScript function.

<!--/**
 * Function for browser information detection
 *
 * @copyright edudip GmbH 2011
 * @author Bodo von der Heiden <b.vonderheiden@edudip.com>
 */--> 
 
function detectUseragend() {
	uAgent = navigator.userAgent;
 
	platform = 'other';
	version = null;
	device = 'PC';
 
	if (-1 != uAgent.toLowerCase().indexOf('android')) {
		platform = 'Android';
	} else if (-1 != uAgent.toLowerCase().indexOf('linux')) {
		platform = 'LINUX';
	} else if (-1 != uAgent.toLowerCase().indexOf('iphone')) {
		platform = 'iOS';
		device = 'iPhone';
	} else if (-1 != uAgent.toLowerCase().indexOf('ipad')) {
		platform = 'iOS';
		device = 'iPad';
	} else if (-1 != uAgent.toLowerCase().indexOf('ipod')) {
		platform = 'iOS';
		device = 'iPod';
	} else if (-1 != uAgent.toLowerCase().indexOf('mac')) {
		platform = 'Mac';
	} else if (-1 != uAgent.toLowerCase().indexOf('win')) {
		platform = 'Windows';
	}
 
	engine = null;
 
	if (-1 != uAgent.toLowerCase().indexOf('webkit')) {
		engine = 'WebKit';
	} else if (-1 != uAgent.toLowerCase().indexOf('gecko')) {
		engine = 'Gecko';
	} else if (-1 != uAgent.toLowerCase().indexOf('trident')) {
		engine = 'Trident';
	}
 
	ub = 'other'
	if (-1 != uAgent.toLowerCase().indexOf('msie') && -1 == uAgent.toLowerCase().indexOf('opera')) {
		ub = 'MSIE';
	} else if (-1 != uAgent.toLowerCase().indexOf('firefox')) {
		ub = 'Firefox';
	} else if (-1 != uAgent.toLowerCase().indexOf('chrome')) {
		ub = 'Chrome';
	} else if (-1 != uAgent.toLowerCase().indexOf('mobile safari')) {
		ub = 'Mobile Safari';
	} else if (-1 != uAgent.toLowerCase().indexOf('safari')) {
		ub = 'Safari';
	} else if (-1 != uAgent.toLowerCase().indexOf('opera')) {
		ub = 'Opera';
	} else if (-1 != uAgent.toLowerCase().indexOf('netscape')) {
		ub = 'other';
	}
 
	browserVersion = '';
	if (-1 != uAgent.toLowerCase().indexOf('version')) {
		browserVersion = uAgent.substr(uAgent.toLowerCase().indexOf('version') + 'version'.length + 1);
	}
 
	if ('' == browserVersion) {	
		if (-1 != uAgent.toLowerCase().indexOf(ub.toLowerCase())) {
			start = uAgent.toLowerCase().indexOf(ub.toLowerCase()) + ub.length + 1;
			browserVersion = uAgent.substr(start);
			if (-1 != browserVersion.indexOf(';')) {
				browserVersion = browserVersion.substr(0, browserVersion.indexOf(';'));
			}
			if (-1 != browserVersion.indexOf(' ')) {
				browserVersion = browserVersion.substr(0, browserVersion.indexOf(' '));
			}
		}
	}
 
	if ('' == browserVersion) {	
		if (-1 != uAgent.toLowerCase().indexOf('other')) {
			browserVersion = uAgent.substr(uAgent.toLowerCase().indexOf('other') + 'other'.length + 1);
		}
	}
 
	os_title = null;
	os_info = uAgent.split(';');
	os_version = null;
 
	$.each (os_info, function(index, part) {
		part = $.trim(part);
		s = platform;
		if (platform == 'iOS')
			s = 'os';
		if (part.toLowerCase().indexOf(s.toLowerCase()) > -1) {
			$.each (part.split(' '), function(index, subpart) {
				subpart = subpart.replace('_', '.');
				if ($.isNumeric(subpart.replace('.', ''))) {
					os_version = subpart;
					os_title = part;
				}
			});
		}
	});
 
	device_build = null;
	device_string = null;
 
	if (platform == 'Android' && count(os_info) > 3) {
		device_string = trim(os_info[count(os_info)-1]);
		device_string_parts = explode('Build/', device_string);
		device_build = trim(device_string_parts[1]);
		device = trim(device_string_parts[0]);
	}
 
	var obj = {};
	obj.string = navigator.userAgent;
 
	obj.browser = {};
	obj.browser.engine = engine;
	obj.browser.name = ub;
	obj.browser.version = browserVersion;
 
	obj.os = {};
	obj.os.platform = platform;
	obj.os.name = os_title;
	obj.os.version = os_version;
 
	obj.device = {};
	obj.device.name = device;
	obj.device.build = device_build;
	obj.device.string = device_string;
 
	return obj;
}

 

To fill the fields in the room, you need the following code:

pInformation = detectUseragend();
osPlatform = pInformation.os.platform;
osVersion = pInformation.os.version;
alert(osPlatform + " - " + osVersion);

 

PHP

To detect the OS, you can use the following PHP class.

/**
 * Class for browser information detection
 *
 * @copyright edudip GmbH 2011
 * @author Robert Saß 
 */
 
class DetectUseragent {
 
	/* Browser */
	const IE = 'MSIE';
	const FIREFOX = 'Firefox';
	const WEBKIT = 'WebKit';
	const SAFARI = 'Safari';
	const CHROMIUM = 'Chomium';
	const CHROME = 'Chrome';
	const OPERA = 'Opera';
	const OTHER = 'other';
 
	/* Mobile Browser */
	const MOBILESAFARI = 'Mobile Safari';
 
	/* Engines */
	const E_WEBKIT = 'WebKit';
	const E_GECKO = 'Gecko';
	const E_TRIDENT = 'Trident';
 
	/* Plattformen */
	const WIN = 'Windows';
	const MAC = 'Mac';
	const LINUX = 'Linux';
 
	/* Mobile Systeme */
	const IOS = 'iOS';
	const ANDROID = 'Android';
 
	/* Geräte */
	const PC = 'PC';
	const IPHONE = 'iPhone';
	const IPAD = 'iPad';
	const IPOD = 'iPod';
 
	public static function detect() {
		$u_agent = $_SERVER['HTTP_USER_AGENT'];
 
		$platform = self::OTHER;
		$version = null;
		$device = self::PC;
 
		if (preg_match('/android/i', $u_agent)) {
			$platform = self::ANDROID;
		} elseif (preg_match('/linux/i', $u_agent)) {
			$platform = self::LINUX;
		} elseif (preg_match('/iphone|iphone os/i', $u_agent)) {
			$platform = self::IOS;
			$device = self::IPHONE;
		} elseif (preg_match('/ipad/i', $u_agent)) {
			$platform = self::IOS;
			$device = self::IPAD;
		} elseif (preg_match('/ipod/i', $u_agent)) {
			$platform = self::IOS;
			$device = self::IPOD;
		} elseif (preg_match('/macintosh|mac os x/i', $u_agent)) {
			$platform = self::MAC;
		} elseif (preg_match('/windows|win32/i', $u_agent)) {
		    $platform = self::WIN;
		}
 
		$engine = null;
 
		if (preg_match('/webkit/i', $u_agent)) {
			$engine = self::E_WEBKIT;
		} elseif (preg_match('/gecko/i', $u_agent)) {
			$engine = self::E_GECKO;
		} elseif (preg_match('/trident/i', $u_agent)) {
			$engine = self::E_TRIDENT;
		}
 
		if (preg_match('/MSIE/i', $u_agent) && !preg_match('/Opera/i', $u_agent)) {
			$ub = self::IE;
		} elseif (preg_match('/Firefox/i', $u_agent)) {
			$ub = self::FIREFOX;
		} elseif (preg_match('/Chrome/i', $u_agent)) {
			$ub = self::CHROME;
		} elseif (preg_match('/Mobile Safari/i', $u_agent)) {
			$ub = self::MOBILESAFARI;
		} elseif (preg_match('/Safari/i', $u_agent)) {
			$ub = self::SAFARI;
		} elseif (preg_match('/Opera/i', $u_agent)) {
		    $ub = self::OPERA;
		} elseif (preg_match('/Netscape/i', $u_agent)) {
			$ub = self::OTHER;
		}
 
		$known = array('Version', $ub, 'other');
		$pattern = '#(?<browser>' . join('|', $known) . ')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
		preg_match_all($pattern, $u_agent, $matches);
 
		$i = count($matches['browser']);
		if ($i != 1) {
		    if (strripos($u_agent, 'Version') < strripos($u_agent, $ub)) {
		        $version = $matches['version'][0];
		    } else {
		        $version = $matches['version'][1];
			}
		} else {
		    $version = $matches['version'][0];
		}
 
		if ($version == null || $version == "") {
			$version = null;
		}
 
		$os_title = null;
		$os_info = explode(';', @array_pop(explode('(', @array_shift(explode(')', $u_agent)))));
		$os_version = null;
 
		foreach ($os_info as $part) {
			$part = trim($part);
			$s = $platform;
			if ($platform == self::IOS)
				$s = 'os';
			if (substr_count(strtolower($part), strtolower($s)) > 0) {
				foreach (explode(' ', $part) as $subpart) {
					$subpart = str_replace('_', '.', $subpart);
					if (is_numeric(str_replace('.', '', $subpart))) {
						$os_version = $subpart;
						$os_title = $part;
						break 2;
					}
				}
			}
		}
 
		$device_build = null;
		$device_string = null;
 
		if ($platform == self::ANDROID && count($os_info) > 3) {
			$device_string = trim($os_info[count($os_info)-1]);
			$device_string_parts = explode('Build/', $device_string);
			$device_build = trim($device_string_parts[1]);
			$device = trim($device_string_parts[0]);
		}
 
		$obj = new stdClass();
		$obj->string = $_SERVER['HTTP_USER_AGENT'];
 
		$obj->browser = new stdClass();
		$obj->browser->engine = $engine;
		$obj->browser->name = $ub;
		$obj->browser->version = $version;
 
		$obj->os = new stdClass();
		$obj->os->platform = $platform;
		$obj->os->name = $os_title;
		$obj->os->version = $os_version;
 
		$obj->device = new stdClass();
		$obj->device->name = $device;
		$obj->device->build = $device_build;
		$obj->device->string = $device_string;
 
		return $obj;
	}
}

 

To fill the fields in the room, you need the following code:

$pInformation = DetectUseragent::detect();
$osPlatform = $pInformation->os->platform;
$osVersion = $pInformation->os->version;
echo $osPlatform . " - " . $osVersion;

Durch die weitere Nutzung der Seite stimmst du der Verwendung von Cookies zu. Weitere Informationen

Die Cookie-Einstellungen auf dieser Website sind auf "Cookies zulassen" eingestellt, um das beste Surferlebnis zu ermöglichen. Wenn du diese Website ohne Änderung der Cookie-Einstellungen verwendest oder auf "Akzeptieren" klickst, erklärst du sich damit einverstanden.

Schließen