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; }