标签:
1、来自CSDN
地址:http://blog.csdn.net/andy1219111/article/details/27543391
服务器端如何判断客户端是不是手机
最近开发我们网站的手机版,采用了这样的模式实现:
根据客户端浏览器user agent判断用户浏览器类型,如果用户使用的是手机访问,则给用户展示手机端模板。注意,我们这里pc端和手机端采用的是同一套系统,
比如pc端网站为http://aaron.com,手机也访问http://aaron.com,只是跟对浏览器类型向客户端发送不同的页面。
废话就到这里,直接上代码,此段代码用来辨别是不是手机端
/** * 判断是否是通过手机访问 * @return bool 是否是移动设备 */ public function isMobile() { //判断手机发送的客户端标志 if(isset($_SERVER[‘HTTP_USER_AGENT‘])) { $userAgent = strtolower($_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‘, ‘opera mobi‘, ‘openwave‘, ‘nexusone‘, ‘cldc‘, ‘midp‘, ‘wap‘, ‘mobile‘ ); // 从HTTP_USER_AGENT中查找手机浏览器的关键字 if(preg_match("/(".implode(‘|‘,$clientkeywords).")/i",$userAgent)&&strpos($userAgent,‘ipad‘) === false) { return true; } } return false; }
2、博客园
地址:http://www.cnblogs.com/scofi/articles/3654098.html
/** * * 根据php的$_SERVER[‘HTTP_USER_AGENT‘] 中各种浏览器访问时所包含各个浏览器特定的字符串来判断是属于PC还是移动端 * @author discuz3x * @lastmodify 2014-04-09 * @return BOOL */ function checkmobile() { global $_G; $mobile = array(); //各个触控浏览器中$_SERVER[‘HTTP_USER_AGENT‘]所包含的字符串数组 static $touchbrowser_list =array(‘iphone‘, ‘android‘, ‘phone‘, ‘mobile‘, ‘wap‘, ‘netfront‘, ‘java‘, ‘opera mobi‘, ‘opera mini‘, ‘ucweb‘, ‘windows ce‘, ‘symbian‘, ‘series‘, ‘webos‘, ‘sony‘, ‘blackberry‘, ‘dopod‘, ‘nokia‘, ‘samsung‘, ‘palmsource‘, ‘xda‘, ‘pieplus‘, ‘meizu‘, ‘midp‘, ‘cldc‘, ‘motorola‘, ‘foma‘, ‘docomo‘, ‘up.browser‘, ‘up.link‘, ‘blazer‘, ‘helio‘, ‘hosin‘, ‘huawei‘, ‘novarra‘, ‘coolpad‘, ‘webos‘, ‘techfaith‘, ‘palmsource‘, ‘alcatel‘, ‘amoi‘, ‘ktouch‘, ‘nexian‘, ‘ericsson‘, ‘philips‘, ‘sagem‘, ‘wellcom‘, ‘bunjalloo‘, ‘maui‘, ‘smartphone‘, ‘iemobile‘, ‘spice‘, ‘bird‘, ‘zte-‘, ‘longcos‘, ‘pantech‘, ‘gionee‘, ‘portalmmm‘, ‘jig browser‘, ‘hiptop‘, ‘benq‘, ‘haier‘, ‘^lct‘, ‘320x320‘, ‘240x320‘, ‘176x220‘); //window手机浏览器数组【猜的】 static $mobilebrowser_list =array(‘windows phone‘); //wap浏览器中$_SERVER[‘HTTP_USER_AGENT‘]所包含的字符串数组 static $wmlbrowser_list = array(‘cect‘, ‘compal‘, ‘ctl‘, ‘lg‘, ‘nec‘, ‘tcl‘, ‘alcatel‘, ‘ericsson‘, ‘bird‘, ‘daxian‘, ‘dbtel‘, ‘eastcom‘, ‘pantech‘, ‘dopod‘, ‘philips‘, ‘haier‘, ‘konka‘, ‘kejian‘, ‘lenovo‘, ‘benq‘, ‘mot‘, ‘soutec‘, ‘nokia‘, ‘sagem‘, ‘sgh‘, ‘sed‘, ‘capitel‘, ‘panasonic‘, ‘sonyericsson‘, ‘sharp‘, ‘amoi‘, ‘panda‘, ‘zte‘); $pad_list = array(‘pad‘, ‘gt-p1000‘); $useragent = strtolower($_SERVER[‘HTTP_USER_AGENT‘]); if(dstrpos($useragent, $pad_list)) { return false; } if(($v = dstrpos($useragent, $mobilebrowser_list, true))){ $_G[‘mobile‘] = $v; return ‘1‘; } if(($v = dstrpos($useragent, $touchbrowser_list, true))){ $_G[‘mobile‘] = $v; return ‘2‘; } if(($v = dstrpos($useragent, $wmlbrowser_list))) { $_G[‘mobile‘] = $v; return ‘3‘; //wml版 } $brower = array(‘mozilla‘, ‘chrome‘, ‘safari‘, ‘opera‘, ‘m3gate‘, ‘winwap‘, ‘openwave‘, ‘myop‘); if(dstrpos($useragent, $brower)) return false; $_G[‘mobile‘] = ‘unknown‘; //对于未知类型的浏览器,通过$_GET[‘mobile‘]参数来决定是否是手机浏览器 if(isset($_G[‘mobiletpl‘][$_GET[‘mobile‘]])) { return true; } else { return false; } } /** * 判断$arr中元素字符串是否有出现在$string中 * @param $string $_SERVER[‘HTTP_USER_AGENT‘] * @param $arr 各中浏览器$_SERVER[‘HTTP_USER_AGENT‘]中必定会包含的字符串 * @param $returnvalue 返回浏览器名称还是返回布尔值,true为返回浏览器名称,false为返回布尔值【默认】 * @author discuz3x * @lastmodify 2014-04-09 */ function dstrpos($string, $arr, $returnvalue = false) { if(empty($string)) return false; foreach((array)$arr as $v) { if(strpos($string, $v) !== false) { $return = $returnvalue ? $v : true; return $return; } } return false; }
/** * * 根据php的$_SERVER[‘HTTP_USER_AGENT‘] 中各种浏览器访问时所包含各个浏览器特定的字符串来判断是属于PC还是移动端 * @author discuz3x * @lastmodify 2014-04-09 * @return BOOL */ function checkmobile() { global $_G; $mobile = array(); //各个触控浏览器中$_SERVER[‘HTTP_USER_AGENT‘]所包含的字符串数组 static $touchbrowser_list =array(‘iphone‘, ‘android‘, ‘phone‘, ‘mobile‘, ‘wap‘, ‘netfront‘, ‘java‘, ‘opera mobi‘, ‘opera mini‘, ‘ucweb‘, ‘windows ce‘, ‘symbian‘, ‘series‘, ‘webos‘, ‘sony‘, ‘blackberry‘, ‘dopod‘, ‘nokia‘, ‘samsung‘, ‘palmsource‘, ‘xda‘, ‘pieplus‘, ‘meizu‘, ‘midp‘, ‘cldc‘, ‘motorola‘, ‘foma‘, ‘docomo‘, ‘up.browser‘, ‘up.link‘, ‘blazer‘, ‘helio‘, ‘hosin‘, ‘huawei‘, ‘novarra‘, ‘coolpad‘, ‘webos‘, ‘techfaith‘, ‘palmsource‘, ‘alcatel‘, ‘amoi‘, ‘ktouch‘, ‘nexian‘, ‘ericsson‘, ‘philips‘, ‘sagem‘, ‘wellcom‘, ‘bunjalloo‘, ‘maui‘, ‘smartphone‘, ‘iemobile‘, ‘spice‘, ‘bird‘, ‘zte-‘, ‘longcos‘, ‘pantech‘, ‘gionee‘, ‘portalmmm‘, ‘jig browser‘, ‘hiptop‘, ‘benq‘, ‘haier‘, ‘^lct‘, ‘320x320‘, ‘240x320‘, ‘176x220‘); //window手机浏览器数组【猜的】 static $mobilebrowser_list =array(‘windows phone‘); //wap浏览器中$_SERVER[‘HTTP_USER_AGENT‘]所包含的字符串数组 static $wmlbrowser_list = array(‘cect‘, ‘compal‘, ‘ctl‘, ‘lg‘, ‘nec‘, ‘tcl‘, ‘alcatel‘, ‘ericsson‘, ‘bird‘, ‘daxian‘, ‘dbtel‘, ‘eastcom‘, ‘pantech‘, ‘dopod‘, ‘philips‘, ‘haier‘, ‘konka‘, ‘kejian‘, ‘lenovo‘, ‘benq‘, ‘mot‘, ‘soutec‘, ‘nokia‘, ‘sagem‘, ‘sgh‘, ‘sed‘, ‘capitel‘, ‘panasonic‘, ‘sonyericsson‘, ‘sharp‘, ‘amoi‘, ‘panda‘, ‘zte‘); $pad_list = array(‘pad‘, ‘gt-p1000‘); $useragent = strtolower($_SERVER[‘HTTP_USER_AGENT‘]); if(dstrpos($useragent, $pad_list)) { return false; } if(($v = dstrpos($useragent, $mobilebrowser_list, true))){ $_G[‘mobile‘] = $v; return ‘1‘; } if(($v = dstrpos($useragent, $touchbrowser_list, true))){ $_G[‘mobile‘] = $v; return ‘2‘; } if(($v = dstrpos($useragent, $wmlbrowser_list))) { $_G[‘mobile‘] = $v; return ‘3‘; //wml版 } $brower = array(‘mozilla‘, ‘chrome‘, ‘safari‘, ‘opera‘, ‘m3gate‘, ‘winwap‘, ‘openwave‘, ‘myop‘); if(dstrpos($useragent, $brower)) return false; $_G[‘mobile‘] = ‘unknown‘; //对于未知类型的浏览器,通过$_GET[‘mobile‘]参数来决定是否是手机浏览器 if(isset($_G[‘mobiletpl‘][$_GET[‘mobile‘]])) { return true; } else { return false; } } /** * 判断$arr中元素字符串是否有出现在$string中 * @param $string $_SERVER[‘HTTP_USER_AGENT‘] * @param $arr 各中浏览器$_SERVER[‘HTTP_USER_AGENT‘]中必定会包含的字符串 * @param $returnvalue 返回浏览器名称还是返回布尔值,true为返回浏览器名称,false为返回布尔值【默认】 * @author discuz3x * @lastmodify 2014-04-09 */ function dstrpos($string, $arr, $returnvalue = false) { if(empty($string)) return false; foreach((array)$arr as $v) { if(strpos($string, $v) !== false) { $return = $returnvalue ? $v : true; return $return; } } return false; }
3、JS解决方案
首先手机上网的IP段都是固定的,所以说一般是通过IP的来区分是PC端还是手机端。
其次,手机浏览器的标准也不一样,各有各的浏览器,一般是HTTP_ACCEPT这个服务器变量中如果包含如下几个字符串,就可以判断是手机
text/vnd.wap.wml
application/vnd.wap.xhtml+xml 。
最后就是关于在web端一般会用js来区分的
function IsPC() { var userAgentInfo = navigator.userAgent; var Agents = new Array("Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"); var flag = true; for (var v = 0; v < Agents.length; v++) { if (userAgentInfo.indexOf(Agents[v]) > 0) { flag = false; break; } } return flag; }
标签:
原文地址:http://www.cnblogs.com/fengbohello/p/4268240.html