找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1361|回复: 0
打印 上一主题 下一主题

PHP检测移动设备类mobile detection使用实例

[复制链接]

2536

主题

2536

帖子

7532

积分

论坛元老

Rank: 8Rank: 8

积分
7532
跳转到指定楼层
楼主
发表于 2018-2-14 05:59:10 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

            目前,一个网站有多个版本是很正常的,如PC版,3G版,移动版等等。根据不同的浏览设备我们需要定向到不同的版本中。不仅如此,我们有时候还需要根据不同的客户端加载不同的CSS,因此我们需要能够检测浏览设备,SO,我们就需要用到“mobile detection”类库。
“mobile detection”是一个轻量级移动设备检测的PHP类库,它采用结合特定的HTTP标头中的User-Agent字符串来检测移动客户端环境。注意,mobile detection 只是一个服务器端(PHP)的检测工具,并不能代替响应式Web设计或其他任何形式的客户端功能检测。
mobile detection 类库下载地址:https://github.com/serbanghita/Mobile-Detect
实例1:根据设备重定向到其他版本
当我们使用移动设备浏览某网站时,需要定向到该网站的移动版,首先将具有检测功能的文件Mobile_Detect.php包含到网页中或主页中,现在我们来实现浏览www.uncletoo.com网站时重定向到m.uncletoo.com中:
[U]复制代码[/U] 代码如下:
/*根据文件位置更改路径信息*/
require_once 'Mobile_Detect.php';
$detect = new Mobile_Detect;
if($detect->isMobile()) {
    header('Location: http://m.uncletoo.com/');
    exit;
}
这是定向到移动网站,下面还有其他形式的重定向:
//所有平板设备
if( $detect->isTablet()) {
}
//是移动但非平板设备
if( $detect->isMobile() && !$detect->isTablet()) {
}
//IOS系统
if( $detect->isiOS()) {
}
//Android系统
if( $detect->isAndroidOS()) {
}
//WindowsPhone系统
if( $detect->isWindowsPhoneOS()) {
}
实例2:根据不同设备加载不同资源
如上所述,我们还可以根据不同的浏览设备加载不同的CSS文件。如:
[U]复制代码[/U] 代码如下:
$detect = new Mobile_Detect;
if($detect->isMobile() || $detect->isTablet()) {
    echo "";
} else {
    echo "";
}
注意,mobile detection是一个移动设备检测平台,随着科技的进步会有不同的设备出现,因此你需要随时更新类库,这样才能保证检测的准确性。

            
            
您可能感兴趣的文章:
  • 简单的移动设备检测PHP脚本代码
  • 通过JavaScript或PHP检测Android设备的代码
  • PHP实现根据设备类型自动跳转相应页面的方法
  • thinkphp判断访客为手机端或PC端的方法
  • PHP代码判断设备是手机还是平板电脑(两种方法)
  • PHP判断手机是IOS还是Android
  • PHP简单判断手机设备的方法
  • PHP简单判断iPhone、iPad、Android及PC设备的方法
            
  • 分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    用户反馈
    客户端