您好,欢迎访问一九零五行业门户网

php怎么识别手机还是pc

php识别手机还是pc的方法:1、通过“function ismobile() {...}”方法判断是否是手机端;2、通过“function isweixin() {...}”方法判断是否是微信内置浏览器。
本文操作环境:windows7系统、php7.1版,dell g3电脑
php怎么识别手机还是pc?
php  判断是手机端还是pc端 ; 判断是否是微信浏览器;js判断是否在微信浏览器打开
1、判断是否是手机端
function ismobile() { // 如果有http_x_wap_profile则一定是移动设备 if (isset($_server['http_x_wap_profile'])) { return true; } // 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息 if (isset($_server['http_via'])) { // 找不到为flase,否则为true return stristr($_server['http_via'], "wap") ? true : false; } // 脑残法,判断手机发送的客户端标志,兼容性有待提高。其中'micromessenger'是电脑微信 if (isset($_server['http_user_agent'])) { $clientkeywords = array('nokia','sony','ericsson','mot','samsung','htc','sgh','lg','sharp','sie-','philips','panasonic','alcatel','lenovo','iphone','ipod','blackberry','meizu','android','netfront','symbian','ucweb','windowsce','palm','operamini','operamobi','openwave','nexusone','cldc','midp','wap','mobile','micromessenger'); // 从http_user_agent中查找手机浏览器的关键字 if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_server['http_user_agent']))) { return true; } } // 协议法,因为有可能不准确,放到最后判断 if (isset ($_server['http_accept'])) { // 如果只支持wml并且不支持html那一定是移动设备 // 如果支持wml和html但是wml在html之前则是移动设备 if ((strpos($_server['http_accept'], 'vnd.wap.wml') !== false) && (strpos($_server['http_accept'], 'text/html') === false || (strpos($_server['http_accept'], 'vnd.wap.wml') < strpos($_server['http_accept'], 'text/html')))) { return true; } } return false;}
2、判断是否是微信内置浏览器
function isweixin() { if (strpos($_server['http_user_agent'], 'micromessenger') !== false) { return true; } else { return false; }}
3、js 判断是否是移动设备打开。
if (browser.versions.mobile) {//判断是否是移动设备打开。browser代码在下面 var ua = navigator.useragent.tolowercase();//获取判断用的对象 if (ua.match(/micromessenger/i) == "micromessenger") { //在微信中打开 } if (ua.match(/weibo/i) == "weibo") { //在新浪微博客户端打开 } if (ua.match(/qq/i) == "qq") { //在qq空间打开 } if (browser.versions.ios) { //是否在ios浏览器打开 } if(browser.versions.android){ //是否在安卓浏览器打开 }} else { //否则就是pc浏览器打开}
4、js 通过以下方法可以判断很多浏览器。包括判断ie浏览器,opera浏览器,苹果浏览器,谷歌浏览器,火狐浏览器等。
var browser = { versions: function () { var u = navigator.useragent, app = navigator.appversion; return { //移动终端浏览器版本信息 trident: u.indexof('trident') > -1, //ie内核 presto: u.indexof('presto') > -1, //opera内核 webkit: u.indexof('applewebkit') > -1, //苹果、谷歌内核 gecko: u.indexof('gecko') > -1 && u.indexof('khtml') == -1, //火狐内核 mobile: !!u.match(/applewebkit.*mobile.*/), //是否为移动终端 ios: !!u.match(/\(i[^;]+;( u;)? cpu.+mac os x/), //ios终端 android: u.indexof('android') > -1 || u.indexof('linux') > -1, //android终端或uc浏览器 iphone: u.indexof('iphone') > -1, //是否为iphone或者qqhd浏览器 ipad: u.indexof('ipad') > -1, //是否ipad webapp: u.indexof('safari') == -1 //是否web应该程序,没有头部与底部 }; }(), language: (navigator.browserlanguage || navigator.language).tolowercase()}
推荐学习:《php视频教程》
以上就是php怎么识别手机还是pc的详细内容。
其它类似信息

推荐信息