php 获取操作系统、浏览器版本信息(持续更新)

清泛原创
一、获取操作系统信息:
/**
 * 获取os信息
 *
 * @return os
 */
function getOS()
{
	$agent = $_SERVER['HTTP_USER_AGENT'];
		$os = false;
	if (eregi('win', $agent) && strpos($agent, '95')){
		$os = 'Windows 95';
	}
	else if (eregi('win 9x', $agent) && strpos($agent, '4.90')){
		$os = 'Windows ME';
	}
	else if (eregi('win', $agent) && ereg('98', $agent)){
		$os = 'Windows 98';
	}
	else if (eregi('win', $agent) && eregi('nt 5.0', $agent)){
		$os = 'Windows 2000';
	}
	//---------------------------------------
	else if (eregi('win', $agent) && eregi('nt 5.1', $agent)){
		$os = 'Windows XP';
	}
	else if (eregi('win', $agent) && eregi('nt 6.0', $agent)){
		$os = 'Windows Vista';
	}
	else if (eregi('win', $agent) && eregi('nt 6.1', $agent)){
		$os = 'Windows 7';
	}
	else if (eregi('win', $agent) && eregi('nt 6.2', $agent)){
		$os = 'Windows 8';
	}
	else if (eregi('win', $agent) && eregi('nt 6.3', $agent)){
		$os = 'Windows 8.1';
	}
	else if (eregi('win', $agent) && eregi('nt 6.4', $agent)){
		$os = 'Windows 10';
	}
	else if (eregi('win', $agent) && eregi('nt 10', $agent)){
		$os = 'Windows 10';
	}
	//---------------------------------------
	else if (eregi('win', $agent) && eregi('nt', $agent)){
		$os = 'Windows NT';
	}
	else if (eregi('win', $agent) && ereg('32', $agent)){
		$os = 'Windows 32';
	}
	else if (eregi('linux', $agent)){
		$os = 'Linux';
	}
	else if (eregi('unix', $agent)){
		$os = 'Unix';
	}
	else if (eregi('sun', $agent) && eregi('os', $agent)){
		$os = 'SunOS';
	}
	else if (eregi('ibm', $agent) && eregi('os', $agent)){
		$os = 'IBM OS/2';
	}
	else if (eregi('Mac', $agent) && eregi('PC', $agent)){
		$os = 'Macintosh';
	}
	else if (eregi('PowerPC', $agent)){
		$os = 'PowerPC';
	}
	else if (eregi('AIX', $agent)){
		$os = 'AIX';
	}
	else if (eregi('HPUX', $agent)){
		$os = 'HPUX';
	}
	else if (eregi('NetBSD', $agent)){
		$os = 'NetBSD';
	}
	else if (eregi('BSD', $agent)){
		$os = 'BSD';
	}
	else if (ereg('OSF1', $agent)){ 
		$os = 'OSF1';
	}
	else if (ereg('IRIX', $agent)){
		$os = 'IRIX';
	}
	else if (eregi('FreeBSD', $agent)){
		$os = 'FreeBSD';
	}
	else if (eregi('teleport', $agent)){
		$os = 'teleport';
	}
	else if (eregi('flashget', $agent)){
		$os = 'flashget';
	}
	else if (eregi('webzip', $agent)){
		$os = 'webzip';
	}
	else if (eregi('offline', $agent)){
		$os = 'offline';
	}
	else {
		$os = 'Unknown';
	}
	return $os;
}
二、获取浏览器信息:
/**
 * 获取浏览器信息
 *
 * @return browser
 */
function getBrowser(){
	$agent=$_SERVER["HTTP_USER_AGENT"];
	if(strpos($agent,'MSIE')!==false || strpos($agent,'rv:11.0')) //ie11判断
		return "ie";
	else if(strpos($agent,'Firefox')!==false)
		return "firefox";
	else if(strpos($agent,'Chrome')!==false)
		return "chrome";
	else if(strpos($agent,'Opera')!==false)
		return 'opera';
	else if((strpos($agent,'Chrome')==false)&&strpos($agent,'Safari')!==false)
		return 'safari';
	else
		return 'unknown';
}
三、获取浏览器版本信息:
/**
 * 获取浏览器版本信息
 *
 * @return browser ver
 */
function getBrowserVer(){
	if (empty($_SERVER['HTTP_USER_AGENT'])){ //当浏览器没有发送访问者的信息的时候
		return 'unknow';
	}
	$agent= $_SERVER['HTTP_USER_AGENT']; 
	if (preg_match('/MSIE\s(\d+)\..*/i', $agent, $regs))
		return $regs[1];
	elseif (preg_match('/FireFox\/(\d+)\..*/i', $agent, $regs))
		return $regs[1];
	elseif (preg_match('/Opera[\s|\/](\d+)\..*/i', $agent, $regs))
		return $regs[1];
	elseif (preg_match('/Chrome\/(\d+)\..*/i', $agent, $regs))
		return $regs[1];
	elseif ((strpos($agent,'Chrome')==false)&&preg_match('/Safari\/(\d+)\..*$/i', $agent, $regs))
		return $regs[1];
	else
		return 'unknow';
}
四、获取IP归属地信息:
/**
 * 获取ip归属地
 *
 * @return ip归属地信息
 */
function getIPLocale($ip) {	
	//用淘宝API获取归属地信息
	$retjson = file_get_contents('http://ip.taobao.com/service/getIpInfo.php?ip='.$ip);
	$info = json_decode($retjson, true);
	$data = $info['data'];
	return array($data['country'], $data['region'], $data['city'], $data['county'], $data['isp']);
}
更多获取IP归属地的api请参考《通过API获取IP信息、IP归属地》。

php 操作系统 浏览器 版本

分享到:
评论加载中,请稍后...
创APP如搭积木 - 创意无限,梦想即时!
回到顶部