使用Mobile_Detect.php:识别是手机端还是PC端访设备类型的开源PHP类使用说明

首先推荐一个php轻量级识别类,Mobile-Detect 专门识别是手机端还是pc端访问网站,这样就可以根据访问的终端类型指向手机浏览器适配的网站还是pc浏览器的网站。
Mobile-Detect官网链接如下MobileDetect
示例链接如下:Mobile-Detect Example(本文后面有释义)
js版下载地址:https://github.com/hgoebl/mobile-detect.js
下面是我写得简单的跳转适配PC端还是手机端的代码:
<?php
require_once 'Mobile_Detect.php'; //注意要引入Mobile_Detect.php  这个类在上文的连接中有下载链接
$detect = new Mobile_Detect;
if ($detect->isMobile())
{
    header('Location: https://www.ecshop.cx/MobileDetect/MobileDetect/mobile.html', true, 301);
    echo "mobile";
}
else
{
    header('Location: https://www.ecshop.cx/MobileDetect/MobileDetect/pc.html', true, 301);
    echo "pc";
}
?>  
<?php
// These lines are mandatory.这些线路是强制性的
require_once 'Mobile_Detect.php';
$detect = new Mobile_Detect;
<?php
// 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);
<?php
// Check for mobile environment.检查是否为 移动环境
if ($detect->isMobile()) {
    // Your code here.
}
<?php
// Check for tablet device.检查是否为 平板设备
if($detect->isTablet()){
    // Your code here.
}
<?php
// Check for any mobile device, excluding tablets.检查任何移动设备,不包括平板
if ($detect->isMobile() && !$detect->isTablet())
{
    // Your code here.
}
22222
<?php
//  Keep the value in $_SESSION for later use and for optimizing the speed of the code.
//保存值session以后使用session和优化代码的速度
if (!$_SESSION['isMobile'])
{
    $_SESSION['isMobile'] = $detect->isMobile();
}
<?php
// Redirect the user to your mobile version of the site.将用户重定向到站点的移动版本
if ($detect->isMobile())
{
    header('https://m.ecshop.cx', true, 301);//改为自己的站点
}
<?php
// 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)
// [...]

本文原创地址:https://www.ecshopok.com/article-606.html
版权所有 © 转载时必须以链接形式注明出处!

觉得本文对您有用,想收藏下来!方法很简单:请点击-〉
我们一直坚持白天工作、晚上熬夜更新资源,付出了巨大的精力和时间,其中的辛酸难以言述。

文章评论

暂无评论,来发表一个吧

发表 取消
充值有惊喜 ECSHOP插件网微信客服edait_cn