文章目录
- userAgent获取方式
- javascript
- php
userAgent获取方式
javascript
JS判断是页面是在手机端还是PC端打开(navigator.userAgent)
<script>console.log(navigator.userAgent);var os = function (){var ua = navigator.userAgent,isWindowsPhone = /(?:Windows Phone)/.test(ua),isSymbian = /(?:SymbianOS)/.test(ua) || isWindowsPhone,isAndroid = /(?:Android)/.test(ua),isFireFox = /(?:Firefox)/.test(ua),isChrome = /(?:Chrome|CriOS)/.test(ua),isTablet = /(?:iPad|PlayBook)/.test(ua) || (isAndroid && !/(?:Mobile)/.test(ua)) || (isFireFox && /(?:Tablet)/.test(ua)),isPhone = /(?:iPhone)/.test(ua) && !isTablet,isPc = !isPhone && !isAndroid && !isSymbian;return {isTablet: isTablet,isPhone: isPhone,isAndroid: isAndroid,isPc: isPc};
}();if (os.isAndroid || os.isPhone) {console.log("手机")
} else if (os.isTablet) {console.log("平板")
} else if(os.isPc) {console.log("电脑")
}
</script>
php
PHP解析HTTP_USER_AGENT 获取客户端浏览器以及版本号
PHP解析HTTP_USER_AGENT 获取客户端手机型号
/*** 获取客户端手机型号* @param $agent //$_SERVER['HTTP_USER_AGENT']* @return array[mobile_brand] 手机品牌* @return array[mobile_ver] 手机型号*/
function getClientMobileBrand($agent = ''){if(preg_match('/iPhone\s([^\s|;]+)/i', $agent, $regs)) {$mobile_brand = '苹果';$mobile_ver = $regs[0];}elseif(preg_match('/SAMSUNG|Galaxy|GT-|SCH-|SM-\s([^\s|;]+)/i', $agent, $regs)) {$mobile_brand = '三星';$mobile_ver = $regs[0];}elseif(preg_match('/Huawei|Honor|H60-|H30-\s([^\s|;]+)/i', $agent, $regs)) {$mobile_brand = '华为';$mobile_ver = $regs[0];}elseif(preg_match('/Mi note|mi one\s([^\s|;]+)/i', $agent, $regs)) { $mobile_brand = '小米';$mobile_ver = $regs[0];}elseif(preg_match('/HM NOTE|HM201\s([^\s|;]+)/i', $agent, $regs)) { $mobile_brand = '红米';$mobile_ver = $regs[0];}elseif(preg_match('/Coolpad|8190Q|5910\s([^\s|;]+)/i', $agent, $regs)) { $mobile_brand = '酷派';$mobile_ver = $regs[0];}elseif(preg_match('/ZTE|X9180|N9180|U9180\s([^\s|;]+)/i', $agent, $regs)) { $mobile_brand = '中兴';$mobile_ver = $regs[0];}elseif(preg_match('/OPPO|X9007|X907|X909|R831S|R827T|R821T|R811|R2017\s([^\s|;]+)/i', $agent, $regs)) { $mobile_brand = 'OPPO';$mobile_ver = $regs[0];}elseif(preg_match('/HTC|Desire\s([^\s|;]+)/i', $agent, $regs)) { $mobile_brand = 'HTC';$mobile_ver = $regs[0];}elseif(preg_match('/Nubia|NX50|NX40\s([^\s|;]+)/i', $agent, $regs)) { $mobile_brand = '努比亚';$mobile_ver = $regs[0];}elseif(preg_match('/M045|M032|M355\s([^\s|;]+)/i', $agent, $regs)) { $mobile_brand = '魅族';$mobile_ver = $regs[0];}elseif(preg_match('/Gionee|GN\s([^\s|;]+)/i', $agent, $regs)) { $mobile_brand = '金立';$mobile_ver = $regs[0];}elseif(preg_match('/HS-U|HS-E\s([^\s|;]+)/i', $agent, $regs)) { $mobile_brand = '海信';$mobile_ver = $regs[0];}elseif(preg_match('/Lenove\s([^\s|;]+)/i', $agent, $regs)) {$mobile_brand = '联想';$mobile_ver = $regs[0];}elseif(preg_match('/ONEPLUS\s([^\s|;]+)/i', $agent, $regs)) {$mobile_brand = '一加';$mobile_ver = $regs[0];}elseif(preg_match('/vivo\s([^\s|;]+)/i', $agent, $regs)) {$mobile_brand = 'vivo';$mobile_ver = $regs[0];}elseif(preg_match('/K-Touch\s([^\s|;]+)/i', $agent, $regs)) {$mobile_brand = '天语';$mobile_ver = $regs[0];}elseif(preg_match('/DOOV\s([^\s|;]+)/i', $agent, $regs)) {$mobile_brand = '朵唯';$mobile_ver = $regs[0];}elseif(preg_match('/GFIVE\s([^\s|;]+)/i', $agent, $regs)) {$mobile_brand = '基伍';$mobile_ver = $regs[0];}elseif(preg_match('/Nokia\s([^\s|;]+)/i', $agent, $regs)) {$mobile_brand = '诺基亚';$mobile_ver = $regs[0];}else{$mobile_brand = '其他';}return ['mobile_brand'=>$mobile_brand, 'mobile_ver'=>$mobile_ver];
}
————————————————
版权声明:本文为CSDN博主「trim(me)」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_37689230/java/article/details/82863036