用php获取访问用户的设备代理信息,可以获取设备类型是移动端还是电脑端、设备的系统类型是windows还是linux等以及用的哪个浏览器甚至手机品牌等。
用php获取访问用户的设备代理信息,可以获取设备类型是移动端还是电脑端、设备的系统类型是windows还是linux等以及用的哪个浏览器甚至手机品牌等。
以下源码获取终端设备是移动端还是电脑端,系统类型,浏览器类型为例:
function getUserAgentInfo(){
//获取user-agent
$userAgent = empty($_SERVER['HTTP_USER_AGENT']) ? 'null' : $_SERVER['HTTP_USER_AGENT'];
//判断访问设备类型
$regex_match="/(nokia|iphone|android|motorola|^mot\-|softbank|foma|docomo|kddi|up\.browser|up\.link|KFAPWI|mobile|";
$regex_match.="htc|dopod|ipad|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|";
$regex_match.="blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam\-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|";
$regex_match.="symbian|smartphone|midp|wap|phone|windows ce|Windows Phone|iemobile|^spice|^bird|^zte\-|longcos|pantech|gionee|^sie\-|portalmmm|";
$regex_match.="jig\s browser|hiptop|^ucweb|^benq|haier|^lct|opera\s*mobi|opera\*mini|320x320|240x320|176x220";
$regex_match.=")/i";
$device = "PC";
if(isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE']) or preg_match($regex_match, strtolower($userAgent))){
$device = 'M';
}
//判断访问系统
$system = "unknown";
$ios_regex="/(iPhone OS|iPad)/i";
if(false !== stripos($userAgent, 'Android')){
$system = "Android";
}else if(false !== stripos($userAgent, 'Windows Phone')){
$system = "Windows Phone";
}else if(false !== stripos($userAgent, 'Windows NT')){
$system = "Windows";
}else if(preg_match($ios_regex, $userAgent)){
$system = "IOS";
}else if(false !== stripos($userAgent, 'Mac')){
$system = "MacOS";
}else if(false !== stripos($userAgent, 'Linux')){
$system = "Linux";
}else if(false !== stripos($userAgent, 'Unix')){
$system = "Unix";
}
//判断访问客户端
$client = "unknown";
$ie_regex="/(MSIE|Trident)/i";
if(stripos($userAgent, 'MicroMessenger')){
$client = "WeiXin";
}else if(stripos($userAgent, 'AlipayClient')){
$client = "Alipay";
}else if(stripos($userAgent, 'QQTheme')){
$client = "QQ";
}else if(stripos($userAgent, 'HeyTapBrowser')){
$client = "HeyTap";
}else if(stripos($userAgent, 'VivoBrowser')){
$client = "Vivo";
}else if(stripos($userAgent, 'MQQBrowser')){
$client = "QQ浏览器移动端";
}else if(stripos($userAgent, 'MQQBrowser')){
$client = "QQ浏览器";
}else if(stripos($userAgent, 'baiduboxapp')){
$client = "百度app";
}else if(stripos($userAgent, 'weibo')){
$client = "WeiBo";
}else if(stripos($userAgent, 'iqiyi')){
$client = "爱奇艺";
}else if(stripos($userAgent, 'aweme')){
$client = "抖音";
}else if(stripos($userAgent, 'kdtUnion_iting')){
$client = "喜马拉雅";
}else if(stripos($userAgent, 'UCBrowser')){
$client = "UC";
}else if(stripos($userAgent, 'DingTalk')){
$client = "钉钉";
}else if(stripos($userAgent, 'SamsungBrowser')){
$client = "三星浏览器";
}else if(stripos($userAgent, 'HUAWEIEVA')){
$client = "华为浏览器";
}else if(stripos($userAgent, 'NokiaBrowser')){
$client = "诺基亚浏览器";
}else if(stripos($userAgent, 'Opera')){
$client = "欧朋浏览器";
}else if(stripos($userAgent, 'Firefox')){
$client = "火狐浏览器";
}else if(stripos($userAgent, 'Chrome')){
$client = "谷歌";
}else if(stripos($userAgent, 'Safari')){
$client = "苹果浏览器";
}else if(preg_match($ie_regex, $userAgent)){
$client = "IE";
}
return Array('device'=>$device, 'system'=>$system, 'client'=>$client);
}