首先推荐一个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';
- $detect = new Mobile_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";
- }
-
-
- ?>
基本 用法 of the Mobile_Detect
class, run the example files https://github.com/serbanghita/Mobile-Detect/tree/master/examples
<?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.
}
<?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('http://m.yoursite.com', 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)
// [...]