您的位置:首页 > 编程语言 > PHP开发

PHP检测终端设备是平板、手机还是电脑

2015-11-17 11:02 651 查看
<?php

$ua = $_SERVER['HTTP_USER_AGENT'];

function userAgent($ua){

$iphone = strstr(strtolower($ua), 'mobile'); //Search for 'mobile' in user-agent (iPhone have that)
$android = strstr(strtolower($ua), 'android'); //Search for 'android' in user-agent
$windowsPhone = strstr(strtolower($ua), 'phone'); //Search for 'phone' in user-agent (Windows Phone uses that)

function androidTablet($ua){ //Find out if it is a tablet
if(strstr(strtolower($ua), 'android') ){//Search for android in user-agent
if(!strstr(strtolower($ua), 'mobile')){ //If there is no ''mobile' in user-agent (Android have that on their phones, but not tablets)
return true;
}
}
}
$androidTablet = androidTablet($ua); //Do androidTablet function
$ipad = strstr(strtolower($ua), 'ipad'); //Search for iPad in user-agent

if($androidTablet || $ipad){ //If it's a tablet (iPad / Android)
return 'tablet';
}
elseif($iphone && !$ipad || $android && !$androidTablet || $windowsPhone){ //If it's a phone and NOT a tablet
return 'mobile';
}
else{ //If it's not a mobile device
return 'desktop';
}
}

echo userAgent($ua);

/* 判断可以继续跳转到对应的域名 */
//判断如果当前设备不是电脑访问,则跳转到wap;如果当前设备是电脑访问,则跳转到www
if(userAgent($ua) != 'desktop'){

  location ='http://wap.baidu.com'+window.location.pathname;
} else {
   location ='http://www.baidu.com'+window.location.pathname;

}

?>


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: