`
天梯梦
  • 浏览: 13764478 次
  • 性别: Icon_minigender_2
  • 来自: 洛杉矶
社区版块
存档分类
最新评论

php 手机系统 php-mobile-detect

 
阅读更多

1. php  mobile detect

 

 

<?php

/**
 * Mobile Detect
 *
 * @license    http://www.opensource.org/licenses/mit-license.php The MIT License
 * @version    SVN: $Id: Mobile_Detect.php 4 2011-05-26 08:04:20Z vic.stanciu@gmail.com $
 */

class Mobile_Detect {
    
    protected $accept;
    protected $userAgent;
    
    protected $isMobile     = false;
    protected $isAndroid    = null;
    protected $isBlackberry = null;
    protected $isOpera      = null;
    protected $isPalm       = null;
    protected $isWindows    = null;
    protected $isGeneric    = null;

    protected $devices = array(
        "android"       => "android",
        "blackberry"    => "blackberry",
        "iphone"        => "(iphone|ipod)",
        "opera"         => "opera mini",
        "palm"          => "(avantgo|blazer|elaine|hiptop|palm|plucker|xiino)",
        "windows"       => "windows ce; (iemobile|ppc|smartphone)",
        "generic"       => "(kindle|mobile|mmp|midp|o2|pda|pocket|psp|symbian|smartphone|treo|up.browser|up.link|vodafone|wap)"
    );


    public function __construct() {
        $this->userAgent = $_SERVER['HTTP_USER_AGENT'];
        $this->accept    = $_SERVER['HTTP_ACCEPT'];

        if (isset($_SERVER['HTTP_X_WAP_PROFILE'])|| isset($_SERVER['HTTP_PROFILE'])) $this->isMobile = true;
		elseif (strpos($this->accept,'text/vnd.wap.wml') > 0 || strpos($this->accept,'application/vnd.wap.xhtml+xml') > 0) $this->isMobile = true;
		else foreach ($this->devices as $device => $regexp) if ($this->isDevice($device)) $this->isMobile = true;
    }


    /**
     * Overloads isAndroid() | isBlackberry() | isOpera() | isPalm() | isWindows() | isGeneric() through isDevice()
     *
     * @param string $name
     * @param array $arguments
     * @return bool
     */
    public function __call($name, $arguments) {
        $device = substr($name, 2);
        if ($name == "is" . ucfirst($device)) return $this->isDevice($device);
        else  trigger_error("Method $name not defined", E_USER_ERROR);
    }


    /**
     * Returns true if any type of mobile device detected, including special ones
     * @return bool
     */
    public function isMobile() {
        return $this->isMobile;
    }


    protected function isDevice($device) {
        $var    = "is" . ucfirst($device);
        $return = $this->$var === null ? (bool) preg_match("/" . $this->devices[$device] . "/i", $this->userAgent) : $this->$var;

        if ($device != 'generic' && $return == true) $this->isGeneric = false;

        return $return;
    }
}
 

 

 

Description

Mobile_Detect is a simple PHP class for easy detection of the most popular mobile devices platforms: Android, Blackberry, Opera Mini, Palm, Windows Mobile, as well as generic ones.

Usage

Include and instantiate the class:

 

include("Mobile_Detect.php");
$detect = new Mobile_Detect();

 

Check for a specific platform:

 

if ($detect->isAndroid()) {
    // code to run for the Google Android platform
}

 

Available methods are isAndroid(), isBlackberry(), isOpera(), isPalm(), isWindows(), isGeneric(). Alternatively, if you are only interested in checking to see if the user is using a mobile device, without caring for specific platform:

 

if ($detect->isMobile()) {
    // any mobile platform
}

 

 

 

来源:http://code.google.com/p/php-mobile-detect/

 

 

2. .htaccess

 

#redirect mobile browsers
RewriteCond %{HTTP_USER_AGENT} ^.*iPhone.*$
RewriteRule ^(.*)$ http://mobile.yourdomain.com [R=301]
RewriteCond %{HTTP_USER_AGENT} ^.*BlackBerry.*$
RewriteRule ^(.*)$ http://mobile.yourdomain.com [R=301]
RewriteCond %{HTTP_USER_AGENT} ^.*Palm.*$
RewriteRule ^(.*)$ http://mobile.yourdomain.com [R=301]
 

或者

 

RewriteCond %{REQUEST_URI} !^/mobiledirectoryhere/.*$
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteRule ^(.*)$ /mobiledirectoryhere/ [L,R=302]
 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

    Mobile-Detect-2.8.12

    Mobile-Detect-2.8.12版本的压缩包中可能包含了类文件、示例代码和其他辅助资源,帮助开发者快速理解和集成这个库。在使用时,务必遵循官方文档或示例,以确保正确无误地引入和使用Mobile-Detect。 总之,Mobile-...

    Mobile-Detect-2.6.9

    《Mobile-Detect-2.6.9:轻松识别移动设备的PHP解决方案》 在现代互联网技术中,响应式设计已经成为网站开发的必备要素,而针对不同设备提供优化的用户体验是关键。Mobile-Detect-2.6.9是一个专门用于检测移动设备...

    Laravel开发-laravel-mobile-detect

    除了基本的设备检测,`laravel-mobile-detect`还提供了许多其他功能,如检测特定品牌和型号的设备,检测设备特性(如触摸屏支持),甚至可以检测设备的操作系统版本。这使得开发者能更精细地控制用户体验,比如根据...

    Mobile-Detect-in-php:检测客户端的设备(MobileTabletPC)

    要使用 `Mobile-Detect-in-php`,你需要先下载并解压文件 `Mobile-Detect-in-php-master`,然后将 `Mobile_Detect.php` 文件引入你的PHP项目中。例如: ```php require_once 'path/to/Mobile_Detect.php'; $detect ...

    Mobile_detect2.7.9

    1. 首先,需要引入Mobile_Detect类文件,通常位于压缩包中的`Mobile-Detect-2.7.9/Mobile_Detect.php`。 ```php require_once 'path/to/Mobile-Detect-2.7.9/Mobile_Detect.php'; ``` 2. 创建一个Mobile_Detect对象...

    Mobile Detect:检测出所用的设备类型和浏览器的详细信息

    Mobile Detect是一个PHP类,通过User-Agent检测各种手机设备,并结合HTTP Header来检测移动设备环境。该类库最强大的地方是,它有一个非常完整的库,可以检测出所用的设备类型(包括操作类型、以及手机品牌等都能...

    mobile_device_detect

    include('sstn/mobile_device_detect.php'); mobile_device_detect(false,true,true,'m/index.php',false); 2、上传附件中的mobile_device_detect.php到sstn目录。没有就在根目录新建一个。 说明。代码中的"m...

    Mobile-Detect:Mobile_Detect是用于检测移动设备(包括平板电脑)的轻量级PHP类。 它结合使用User-Agent字符串和特定的HTTP标头来检测移动环境

    Mobile Detect是用于检测移动设备(包括平板电脑)的轻量级PHP类。 它结合使用User-Agent字符串和特定的HTTP标头来检测移动环境。 为什么 您网站的内容策略很重要! 您需要一个完整的工具包来提供经过优化,快速且...

    mobile-detect:用于检测移动设备,管理移动视图类型,重定向到Nette Framework(2.4+)的移动版本的扩展

    安装ipub / mobile-detect的最佳方法是使用 : $ composer require ipub/mobile-detect 之后,您必须在config.neon中注册扩展名。 extensions : mobileDetect : IPub\MobileDetect\DI\MobileDetectExtension ...

    Mobile-Detect-2.6.4.zip_WEB开发_PHP_

    Mobile-Detect-2.6.4.zip包含了这个库的最新版本2.6.4,它支持识别众多设备,如iPhone、iPod、Windows Phone、Android设备以及传统的个人电脑。 在PHP中,Mobile-Detect通过分析用户浏览器的User-Agent字符串来判断...

    php判断手机访问

    4. **第三方库**:为了更准确地识别各种设备,可以使用第三方库,如提供的"Mobile-Detect-2.8.31"。这是一个轻量级的PHP类,它包含了大量预定义的设备模式,可以识别超过1500种不同的手机型号。使用这个库,只需引入...

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

    此外,Mobile Detect类库也需要定期更新,以纳入最新的设备和操作系统信息,确保检测结果的准确性。这是因为移动设备市场更新迅速,不断有新设备和新系统出现,而User-Agent字符串也会随之变化。 在实现不同设备...

    检测各种移动终端类型

    在这个话题中,我们将深入探讨如何利用PHP进行移动终端检测,并以“Mobile-Detect-2.8.0”这个库为例进行解析。 Mobile-Detect是一个用PHP编写的轻量级类库,它专门用于检测移动设备,包括手机、平板电脑以及现代...

    Mobile解析字符串的PHP库.zip

    'version' =&gt; $detect-&gt;getScriptVersion(), // All headers that trigger 'isMobile' to be 'true', // before reaching the User-Agent match detection. 'headerMatch' =&gt; $detect-&gt;...

    Mobile_Detect是用于检测移动设备(包括平板电脑)的轻量级PHP类。 它结合使用User-Agent字符串和特定的HTTP标头来检测移动环境。-PHP开发

    关于Mobile Detect是用于检测移动设备(包括平板电脑)的轻量级PHP类。 它使用User-Agent字符串组合格言:“每个企业都应该具有检测脚本来检测移动阅读器。” 关于Mobile Detect是用于检测移动设备(包括平板电脑)...

    php手机端访问网站和平板电脑登录判断

    Mobile-Detect-2.6.4是一个流行的PHP库,专门用于检测设备类型,它提供了丰富的功能来识别各种移动设备。 Mobile-Detect库基于HTTP_USER_AGENT头信息来判断设备类型,这是一个服务器接收到的HTTP请求头,其中包含了...

    PHP获取手机型号与系统型号代码

    在PHP编程中,获取手机或PC的型号与系统型号对于开发者来说是非常实用的功能,尤其是在进行设备适配或者数据分析时。本文将深入探讨如何利用PHP实现这些功能。 首先,我们要了解PHP是一个服务器端脚本语言,主要...

    判断web访问者是否手机用户

    if ($detect-&gt;isMobile() && !$detect-&gt;isTablet()) { echo "访问者使用的是手机设备"; } else { echo "访问者可能是桌面用户或平板用户"; } ``` 在这段代码中,我们首先引入了detectmobilebrowsers库,然后创建...

    Mobile Detect:检测出所用的设备类型和浏览器的详细信息.zip

    Mobile Detect是一个PHP类,通过User-Agent检测各种手机设备,并结合HTTP Header来检测移动设备环境。该类库最强大的地方是,它有一个非常完整的库,可以检测出所用的设备类型(包括操作类型、以及手机品牌等都能...

    php检测用户是否用手机(Mobile)访问网站的类

    下载地址http://code.google.com/p/php-mobile-detect/ 程序就是一个文件,下载之后直接引用就...if ($detect-&gt;isMobile()) { // any mobile platform echo ‘isMobile’;}else{ echo ‘isPC’;}?&gt; 您可能感兴趣

Global site tag (gtag.js) - Google Analytics