分享一个php检测移动设备的类Mobile Detect
时间:2016-06-21
在php开发过程中,我们可能需要使用php来判断用户使用的什么设备(手机mobile、ipad平板、pc电脑)访问网站的,这个时候我们可以使用一个轻量级的PHP类(Mobile Detect)来完成。下面我们来介绍一下这个php类:
Mobile Detect是检测移动设备(包括平板电脑)一个轻量级的PHP类。它使用的User-Agent字符串与特定的HTTP标头组合,以检测移动环境。
您可以考虑这个脚本是RESS(与服务器端组件响应网页设计)的一部分。通过阅读这些文章,你可以学到更多关于响应式的知识:
Mobile Detect下载地址:https://github.com/serbanghita/Mobile-Detect/archive/2.8.22.zip
Mobile Detect演示地址:http://demo.mobiledetect.net/
下面是Mobile Detect使用方法示例:
// Include and instantiate the class.
require_once 'Mobile_Detect.php';
$detect = new Mobile_Detect;
// Any mobile device (phones or tablets).
if ( $detect->isMobile() ) {
}
// Any tablet device.
if( $detect->isTablet() ){
}
// Exclude tablets.
if( $detect->isMobile() && !$detect->isTablet() ){
}
// Check for a specific platform with the help of the magic methods:
if( $detect->isiOS() ){
}
if( $detect->isAndroidOS() ){
}
// Alternative method is() for checking specific properties.
// WARNING: this method is in BETA, some keyword properties will change in the future.
$detect->is('Chrome')
$detect->is('iOS')
$detect->is('UC Browser')
// [...]
// Batch mode using setUserAgent():
$userAgents = array(
'Mozilla/5.0 (Linux; Android 4.0.4; Desire HD Build/IMM76D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19',
'BlackBerry7100i/4.1.0 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/103',
// [...]
);
foreach($userAgents as $userAgent){
$detect->setUserAgent($userAgent);
$isMobile = $detect->isMobile();
$isTablet = $detect->isTablet();
// Use the force however you want.
}
// Get the version() of components.
// WARNING: this method is in BETA, some keyword properties will change in the future.
$detect->version('iPad'); // 4.3 (float)
$detect->version('iPhone') // 3.1 (float)
$detect->version('Android'); // 2.1 (float)
$detect->version('Opera Mini'); // 5.0 (float)
// [...]
- swingbench压测Oracle小记(r12笔记第20天)
- 分享最近对网站外链跳转页面代码的一些改善
- swingbench压测Oracle小记(r12笔记第19天)
- Oracle闪回原理测试(三)(r12笔记第16天)
- 利用JS生成二维码图片,优化WEB性能及页面加载速度
- Go语言的指针 & *
- MySQL中的double write(二)(r12笔记第17天)
- 论Go语言中goroutine的使用
- WordPress发布文章同步到新浪微博失败的问题解决与分享
- 压测工具swingbench和sysbench对比(r12笔记第13天)
- 解决WordPress4.4.1不支持图片暗箱问题,Begin主题适用
- MySQL源码安装总结(r12笔记第12天)
- 全站启用SSL之后,如何兼容不支持https抓取的搜索引擎?
- sandbox和MHA快速测试(r12笔记第32天)
- php概述
- php教程
- php环境搭建
- PHP书写格式
- php变量
- php常量
- PHP注释
- php数组
- php字符串 string
- PHP整型 integer
- PHP浮点型 float
- php布尔型
- php数据类型之数组
- php数据类型之对象
- php数据类型之null
- php数据类型之间的转换
- php运算符
- php表达式
- PHP循环控制
- PHP流程控制
- php函数
- php全局变量
- PHP魔术变量
- php命名空间
- php 日期
- PHP包含文件
- php文件
- PHP 文件上传
- php Cookies
- php Sessions
- php email
- php安全email
- php错误处理
- PHP异常处理
- php过滤器
- PHP 高级过滤器
- php json
- php 表单
- PHP MySQL 简介
- PHP 连接 MySQL
- php创建数据库
- php 创建表
- php mysq 插入数据
- PHP MySQL 插入多条数据
- PHP MySQL 预处理语句
- php mysql 读取数据
- php mysql where
- PHP MySQL Order By
- PHP MySQL Update
- PHP MySQL Delete
- php ODBC
- DiDi Kafka-Manager安装和简单使用
- Docker数据管理与挂载管理
- Docker Dockerfile 指令详解与实战案例
- flume kafka和sparkstreaming整合
- Docker如何搭建私有registry镜像仓库
- Harbor介绍与企业级私有Docker镜像仓库搭建
- 如何查看docker run启动参数命令
- YAML 语言教程与使用案例
- 计算等压面要素场的基本检验指标
- Kubernetes K8S之SSL证书有效期修改
- Kubernetes K8S之通过yaml文件创建Pod与Pod常用字段详解
- Kubernetes K8S之kubectl命令详解及常用示例
- Kubernetes K8S之Pod 生命周期与init container初始化容器详解
- Kubernetes K8S之Pod生命周期与探针检测
- Kubernetes K8S之Pod 生命周期与postStart、preStop事件