获取操作系统信息

我还无法找出如何使用PHP进行操作,并想尝试找出答案。

我注意到他们列出了user-agent,其中提供了有关浏览器的大量信息。他们是从中获取操作系统信息还是从别的东西获取操作系统信息?有没有我可以用来获取用户操作系统的API?

我知道他们是如何获得浏览器和IP的,但无法弄清操作系统部分!

回答:

下面的代码可以单独说明如何显示某人正在使用哪个操作系统。

它的作用是嗅探您的核心操作系统模型,例如windows nt 5.1作为我自己的模型。

然后,它将Windows nt 5.1 / i传递给Windows XP作为操作系统。

使用:'/windows nt 5.1/i' => 'Windows XP',来自数组。

您可以说是猜测,也可以是近似值,但还是很不错。

<?php

$user_agent = $_SERVER['HTTP_USER_AGENT'];

function getOS() {

global $user_agent;

$os_platform = "Unknown OS Platform";

$os_array = array(

'/windows nt 10/i' => 'Windows 10',

'/windows nt 6.3/i' => 'Windows 8.1',

'/windows nt 6.2/i' => 'Windows 8',

'/windows nt 6.1/i' => 'Windows 7',

'/windows nt 6.0/i' => 'Windows Vista',

'/windows nt 5.2/i' => 'Windows Server 2003/XP x64',

'/windows nt 5.1/i' => 'Windows XP',

'/windows xp/i' => 'Windows XP',

'/windows nt 5.0/i' => 'Windows 2000',

'/windows me/i' => 'Windows ME',

'/win98/i' => 'Windows 98',

'/win95/i' => 'Windows 95',

'/win16/i' => 'Windows 3.11',

'/macintosh|mac os x/i' => 'Mac OS X',

'/mac_powerpc/i' => 'Mac OS 9',

'/linux/i' => 'Linux',

'/ubuntu/i' => 'Ubuntu',

'/iphone/i' => 'iPhone',

'/ipod/i' => 'iPod',

'/ipad/i' => 'iPad',

'/android/i' => 'Android',

'/blackberry/i' => 'BlackBerry',

'/webos/i' => 'Mobile'

);

foreach ($os_array as $regex => $value)

if (preg_match($regex, $user_agent))

$os_platform = $value;

return $os_platform;

}

function getBrowser() {

global $user_agent;

$browser = "Unknown Browser";

$browser_array = array(

'/msie/i' => 'Internet Explorer',

'/firefox/i' => 'Firefox',

'/safari/i' => 'Safari',

'/chrome/i' => 'Chrome',

'/edge/i' => 'Edge',

'/opera/i' => 'Opera',

'/netscape/i' => 'Netscape',

'/maxthon/i' => 'Maxthon',

'/konqueror/i' => 'Konqueror',

'/mobile/i' => 'Handheld Browser'

);

foreach ($browser_array as $regex => $value)

if (preg_match($regex, $user_agent))

$browser = $value;

return $browser;

}

$user_os = getOS();

$user_browser = getBrowser();

$device_details = "<span>Browser: </span>".$user_browser."<br /><span>Operating System: </span>".$user_os."";

print_r($device_details);

echo("<br /><br /><br />".$_SERVER['HTTP_USER_AGENT']."");

?>


附加代码:

/* return Operating System */

function operating_system_detection(){

if ( isset( $_SERVER ) ) {

$agent = $_SERVER['HTTP_USER_AGENT'];

}

else {

global $HTTP_SERVER_VARS;

if ( isset( $HTTP_SERVER_VARS ) ) {

$agent = $HTTP_SERVER_VARS['HTTP_USER_AGENT'];

}

else {

global $HTTP_USER_AGENT;

$agent = $HTTP_USER_AGENT;

}

}

$ros[] = array('Windows XP', 'Windows XP');

$ros[] = array('Windows NT 5.1|Windows NT5.1)', 'Windows XP');

$ros[] = array('Windows 2000', 'Windows 2000');

$ros[] = array('Windows NT 5.0', 'Windows 2000');

$ros[] = array('Windows NT 4.0|WinNT4.0', 'Windows NT');

$ros[] = array('Windows NT 5.2', 'Windows Server 2003');

$ros[] = array('Windows NT 6.0', 'Windows Vista');

$ros[] = array('Windows NT 7.0', 'Windows 7');

$ros[] = array('Windows CE', 'Windows CE');

$ros[] = array('(media center pc).([0-9]{1,2}\.[0-9]{1,2})', 'Windows Media Center');

$ros[] = array('(win)([0-9]{1,2}\.[0-9x]{1,2})', 'Windows');

$ros[] = array('(win)([0-9]{2})', 'Windows');

$ros[] = array('(windows)([0-9x]{2})', 'Windows');

// Doesn't seem like these are necessary...not totally sure though..

//$ros[] = array('(winnt)([0-9]{1,2}\.[0-9]{1,2}){0,1}', 'Windows NT');

//$ros[] = array('(windows nt)(([0-9]{1,2}\.[0-9]{1,2}){0,1})', 'Windows NT'); // fix by bg

$ros[] = array('Windows ME', 'Windows ME');

$ros[] = array('Win 9x 4.90', 'Windows ME');

$ros[] = array('Windows 98|Win98', 'Windows 98');

$ros[] = array('Windows 95', 'Windows 95');

$ros[] = array('(windows)([0-9]{1,2}\.[0-9]{1,2})', 'Windows');

$ros[] = array('win32', 'Windows');

$ros[] = array('(java)([0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,2})', 'Java');

$ros[] = array('(Solaris)([0-9]{1,2}\.[0-9x]{1,2}){0,1}', 'Solaris');

$ros[] = array('dos x86', 'DOS');

$ros[] = array('unix', 'Unix');

$ros[] = array('Mac OS X', 'Mac OS X');

$ros[] = array('Mac_PowerPC', 'Macintosh PowerPC');

$ros[] = array('(mac|Macintosh)', 'Mac OS');

$ros[] = array('(sunos)([0-9]{1,2}\.[0-9]{1,2}){0,1}', 'SunOS');

$ros[] = array('(beos)([0-9]{1,2}\.[0-9]{1,2}){0,1}', 'BeOS');

$ros[] = array('(risc os)([0-9]{1,2}\.[0-9]{1,2})', 'RISC OS');

$ros[] = array('os/2', 'OS/2');

$ros[] = array('freebsd', 'FreeBSD');

$ros[] = array('openbsd', 'OpenBSD');

$ros[] = array('netbsd', 'NetBSD');

$ros[] = array('irix', 'IRIX');

$ros[] = array('plan9', 'Plan9');

$ros[] = array('osf', 'OSF');

$ros[] = array('aix', 'AIX');

$ros[] = array('GNU Hurd', 'GNU Hurd');

$ros[] = array('(fedora)', 'Linux - Fedora');

$ros[] = array('(kubuntu)', 'Linux - Kubuntu');

$ros[] = array('(ubuntu)', 'Linux - Ubuntu');

$ros[] = array('(debian)', 'Linux - Debian');

$ros[] = array('(CentOS)', 'Linux - CentOS');

$ros[] = array('(Mandriva).([0-9]{1,3}(\.[0-9]{1,3})?(\.[0-9]{1,3})?)', 'Linux - Mandriva');

$ros[] = array('(SUSE).([0-9]{1,3}(\.[0-9]{1,3})?(\.[0-9]{1,3})?)', 'Linux - SUSE');

$ros[] = array('(Dropline)', 'Linux - Slackware (Dropline GNOME)');

$ros[] = array('(ASPLinux)', 'Linux - ASPLinux');

$ros[] = array('(Red Hat)', 'Linux - Red Hat');

// Loads of Linux machines will be detected as unix.

// Actually, all of the linux machines I've checked have the 'X11' in the User Agent.

//$ros[] = array('X11', 'Unix');

$ros[] = array('(linux)', 'Linux');

$ros[] = array('(amigaos)([0-9]{1,2}\.[0-9]{1,2})', 'AmigaOS');

$ros[] = array('amiga-aweb', 'AmigaOS');

$ros[] = array('amiga', 'Amiga');

$ros[] = array('AvantGo', 'PalmOS');

//$ros[] = array('(Linux)([0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,3}(rel\.[0-9]{1,2}){0,1}-([0-9]{1,2}) i([0-9]{1})86){1}', 'Linux');

//$ros[] = array('(Linux)([0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,3}(rel\.[0-9]{1,2}){0,1} i([0-9]{1}86)){1}', 'Linux');

//$ros[] = array('(Linux)([0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,3}(rel\.[0-9]{1,2}){0,1})', 'Linux');

$ros[] = array('[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,3})', 'Linux');

$ros[] = array('(webtv)/([0-9]{1,2}\.[0-9]{1,2})', 'WebTV');

$ros[] = array('Dreamcast', 'Dreamcast OS');

$ros[] = array('GetRight', 'Windows');

$ros[] = array('go!zilla', 'Windows');

$ros[] = array('gozilla', 'Windows');

$ros[] = array('gulliver', 'Windows');

$ros[] = array('ia archiver', 'Windows');

$ros[] = array('NetPositive', 'Windows');

$ros[] = array('mass downloader', 'Windows');

$ros[] = array('microsoft', 'Windows');

$ros[] = array('offline explorer', 'Windows');

$ros[] = array('teleport', 'Windows');

$ros[] = array('web downloader', 'Windows');

$ros[] = array('webcapture', 'Windows');

$ros[] = array('webcollage', 'Windows');

$ros[] = array('webcopier', 'Windows');

$ros[] = array('webstripper', 'Windows');

$ros[] = array('webzip', 'Windows');

$ros[] = array('wget', 'Windows');

$ros[] = array('Java', 'Unknown');

$ros[] = array('flashget', 'Windows');

// delete next line if the script show not the right OS

//$ros[] = array('(PHP)/([0-9]{1,2}.[0-9]{1,2})', 'PHP');

$ros[] = array('MS FrontPage', 'Windows');

$ros[] = array('(msproxy)/([0-9]{1,2}.[0-9]{1,2})', 'Windows');

$ros[] = array('(msie)([0-9]{1,2}.[0-9]{1,2})', 'Windows');

$ros[] = array('libwww-perl', 'Unix');

$ros[] = array('UP.Browser', 'Windows CE');

$ros[] = array('NetAnts', 'Windows');

$file = count ( $ros );

$os = '';

for ( $n=0 ; $n<$file ; $n++ ){

if ( preg_match('/'.$ros[$n][0].'/i' , $agent, $name)){

$os = @$ros[$n][1].' '.@$name[2];

break;

}

}

return trim ( $os );

}


编辑:2015年4月12日

昨天我注意到一个问题,该问题可能与此次问答有关,并且可能对某些问题有所帮助。关于:

Mozilla/5.0 (Linux; Android 4.4.2; SAMSUNG-GT-I9505 Build/KOT49H)

AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.96 Mobile

Safari/537.36

以上是 获取操作系统信息 的全部内容, 来源链接: utcz.com/qa/407338.html

回到顶部