前端集合 - 关注前端技术和互联网免费资源

关注前端技术和互联网免费资源

分类儿

页面儿

搜索儿

订阅儿

前端集合 RSS订阅
Home » 技巧应用 » 3种检测移动设备的方法

3种检测移动设备的方法

发布者:前端集合 // 发布时间:2013-02-18 22:09:00 // 分类:技巧应用 // 访问: 1,976 次 // 热度: 0人参与

现在响应式设计越来越流行, 而且手机, tablet发展越来越迅速, 所以我们在工作中或闲暇折腾网站时会需要检测来访者是否使用移动设备.

本文收集了3中检测方法, 都是基于PHP的:

第一种

function is_mobile() {
	$user_agent = $_SERVER['HTTP_USER_AGENT'];
	$mobile_agents = Array("240x320","acer","acoon","acs-","abacho","ahong","airness","alcatel","amoi","android","anywhereyougo.com","applewebkit/525","applewebkit/532","asus","audio","au-mic","avantogo","becker","benq","bilbo","bird","blackberry","blazer","bleu","cdm-","compal","coolpad","danger","dbtel","dopod","elaine","eric","etouch","fly ","fly_","fly-","go.web","goodaccess","gradiente","grundig","haier","hedy","hitachi","htc","huawei","hutchison","inno","ipad","ipaq","ipod","jbrowser","kddi","kgt","kwc","lenovo","lg ","lg2","lg3","lg4","lg5","lg7","lg8","lg9","lg-","lge-","lge9","longcos","maemo","mercator","meridian","micromax","midp","mini","mitsu","mmm","mmp","mobi","mot-","moto","nec-","netfront","newgen","nexian","nf-browser","nintendo","nitro","nokia","nook","novarra","obigo","palm","panasonic","pantech","philips","phone","pg-","playstation","pocket","pt-","qc-","qtek","rover","sagem","sama","samu","sanyo","samsung","sch-","scooter","sec-","sendo","sgh-","sharp","siemens","sie-","softbank","sony","spice","sprint","spv","symbian","tablet","talkabout","tcl-","teleca","telit","tianyu","tim-","toshiba","tsm","up.browser","utec","utstar","verykool","virgin","vk-","voda","voxtel","vx","wap","wellco","wig browser","wii","windows ce","wireless","xda","xde","zte");
	$is_mobile = false;
	foreach ($mobile_agents as $device) {
		if (stristr($user_agent, $device)) {
			$is_mobile = true;
			break;
		}
	}
	return $is_mobile;
}
if( is_mobile() ){
Your Code
}

第二种: 使用php库 Mobile_Detect

使用方法

a. 去这里下载Mobile_Detect

b. Include 文件

<?php
include 'Mobile_Detect.php';
$detect = new Mobile_Detect();

c. 使用

比如, 检测设备是否为手机

<?php
if ($detect->isMobile()) {
    // Any mobile device.
}

或者检测设备是否为平板

<?php
if($detect->isTablet()){
    // Any tablet device.
}

同时,Mobile_Detect还提供了很多检测类型, 例如:


isiPhone()
bool(false)

isBlackBerry()
bool(false)

isHTC()
bool(false)

isNexus()
bool(false)

isDell()
bool(false)

...

第三种

<?php
// check if wap
function check_wap(){
    if(stristr($_SERVER['HTTP_VIA'],"wap")){
        return true;
    }elseif(strpos(strtoupper($_SERVER['HTTP_ACCEPT']),"VND.WAP.WML") > 0){
        return true;
   }elseif(preg_match('/(blackberry|configuration\/cldc|hp |hp-|htc |htc_|htc-|iemobile|kindle|midp|mmp|motorola|mobile|nokia|opera mini|opera |Googlebot-Mobile|YahooSeeker\/M1A1-R2D2|android|iphone|ipod|mobi|palm|palmos|pocket|portalmmm|ppc;|smartphone|sonyericsson|sqh|spv|symbian|treo|up.browser|up.link|vodafone|windows ce|xda |xda_)/i', $_SERVER['HTTP_USER_AGENT'])){//check USER_AGENT
        return true;            
    }else{
        return false;   
   }
  
}

Tags: 手机, 检测移动设备, 移动设备检测, 手机访问检测, 访问检测, 访客检测, 移动设备, 平板

如何直接在github上预览html网页效果>>  << 那些年, 我用过的代码编辑器
Top