php detect tablet,Mobile_Detect.php:判断移动设备类型的开源PHP类使用说明

首先推荐一个php轻量级识别类,Mobile-Detect 专门识别是手机端还是pc端访问网站,这样就可以根据访问的终端类型指向手机浏览器适配的网站还是pc浏览器的网站。

Mobile-Detect官网链接如下MobileDetect

下面是我写得简单的跳转适配PC端还是手机端的代码:

require_once'Mobile_Detect.php';//注意要引入Mobile_Detect.php  这个类在上文的连接中有下载链接

$detect=newMobile_Detect;

if($detect->isMobile()){

header('Location: http://127.0.0.1/MobileDetect/MobileDetect/mobile.html', true, 301);

echo"mobile";

}else{

header('Location: http://127.0.0.1/MobileDetect/MobileDetect/pc.html', true, 301);

echo"pc";

}

?>

测试你 的 浏览器 或 移动 设备 , 并 检查 该 结果 。

简单切换例子mobile和classic页面 视图 之间切换.

Mini tutorial in Spanish by Alejandro Palop.

// These lines are mandatory.这些线路是强制性的

require_once 'Mobile_Detect.php';

$detect = new Mobile_Detect;

// Basic detection.基本 检测

$detect->isMobile();

$detect->isTablet();

// Magic methods.魔术方法

$detect->isIphone();

$detect->isSamsung();

// [...]

// Alternative to magic methods. 助手方法

$detect->is('iphone');

// Find the version of component.找到组件的版本

$detect->version('Android');

// Additional match method.额外的正则匹配方法

$detect->match('regex.*here');

// Browser grade method.浏览器的分级方法$detect->mobileGrade();

// Batch methods.批量化方法

$detect->setUserAgent($userAgent);

$detect->setHttpHeaders($httpHeaders);

// Check for mobile environment.检查是否为 移动环境if ($detect->isMobile()) {

// Your code here.

}

// Check for tablet device.检查是否为 平板设备

if($detect->isTablet()){

// Your code here.

}

// Check for any mobile device, excluding tablets.检查任何移动设备,不包括平板

if ($detect->isMobile() && !$detect->isTablet()) {

// Your code here.

}

//保存值session以后使用session和优化代码的速度

if(!$_SESSION['isMobile']){

$_SESSION['isMobile'] = $detect->isMobile();

}

// Redirect the user to your mobile version of the site.将用户重定向到站点的移动版本

if($detect->isMobile()){

header('http://m.yoursite.com', true, 301);//改为自己的站点

}

// 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.用is()方法检测特殊属性

// WARNING: this method is in BETA, some keyword properties will change in the future.

//警告:此方法处于测试阶段,某些关键字属性将在未来更改。

$detect->is('Chrome')$detect->is('iOS')$detect->is('UCBrowser')$detect->is('Opera')// [...]// Batch mode using setUserAgent():使用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)// [...]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值