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

使用PHP判断Web客户端访问设备类型(手机、平板、PC)的方法

2016-03-30 15:37 701 查看
目前,一个网站有多个版本是很正常的,如PC端,手机端,平板端等。根据不同的浏览设备我们需要定向到不同的版本中。不仅如此,我们有时候还需要根据不同的客户端加载不同的CSS,因此我们需要能够检测浏览设备。“Mobile Detection”是一个轻量级移动设备检测的PHP类库,它采用结合特定的HTTP标头中的User-Agent字符串来检测移动客户端环境。注意,mobile detection 只是一个服务器端(PHP)的检测工具,并不能代替响应式Web设计或其他任何形式的客户端功能检测。Mobile Detection 类库下载地址:https://github.com/serbanghita/Mobile-Detect当我们使用移动设备浏览某网站时,需要定向到该网站的移动版,首先将具有检测功能的文件Mobile_Detect.php包含到网页中或主页中,现在我们来实现浏览www.example.com网站时重定向到m.example.com中
require_once "Mobile_Detect.php";
$detect = new Mobile_Detect;
if($detect->isMobile() && !$detect->isTablet()){
// 所有手机端
header("Location: http://m.example.com/"); exit;
}else if($detect->isTablet()){
// 所有平板设备
}else if($detect->isiOS()){
// IOS系统
}else if($detect->isAndroidOS()){
// Android系统
}else if($detect->isWindowsPhoneOS()){
//WindowsPhone系统
}
注意:mobile detection是一个移动设备检测的PHP类库,随着不同的新设备出现,因此你需要随时更新类库,这样才能保证检测的准确性。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  php判断移动设备