php - 检测手机浏览器

标签 php mobile detect

<分区>

Possible Duplicate:
Simplest way to detect a mobile device

我有一个站点,我想检测使用的浏览器并重定向它们。 我有一个 php 索引,代码必须在 php 中。 我找到了很多网站,但它们无法正常工作,或者它们检测不到很多移动浏览器。 您是否知道可以检测许多移动浏览器的任何好的代码或教程?

最佳答案

有我的用户代理代码:

<?php

/* USER-AGENTS
================================================== */
function check_user_agent ( $type = NULL ) {
        $user_agent = strtolower ( $_SERVER['HTTP_USER_AGENT'] );
        if ( $type == 'bot' ) {
                // matches popular bots
                if ( preg_match ( "/googlebot|adsbot|yahooseeker|yahoobot|msnbot|watchmouse|pingdom\.com|feedfetcher-google/", $user_agent ) ) {
                        return true;
                        // watchmouse|pingdom\.com are "uptime services"
                }
        } else if ( $type == 'browser' ) {
                // matches core browser types
                if ( preg_match ( "/mozilla\/|opera\//", $user_agent ) ) {
                        return true;
                }
        } else if ( $type == 'mobile' ) {
                // matches popular mobile devices that have small screens and/or touch inputs
                // mobile devices have regional trends; some of these will have varying popularity in Europe, Asia, and America
                // detailed demographics are unknown, and South America, the Pacific Islands, and Africa trends might not be represented, here
                if ( preg_match ( "/phone|iphone|itouch|ipod|symbian|android|htc_|htc-|palmos|blackberry|opera mini|iemobile|windows ce|nokia|fennec|hiptop|kindle|mot |mot-|webos\/|samsung|sonyericsson|^sie-|nintendo/", $user_agent ) ) {
                        // these are the most common
                        return true;
                } else if ( preg_match ( "/mobile|pda;|avantgo|eudoraweb|minimo|netfront|brew|teleca|lg;|lge |wap;| wap /", $user_agent ) ) {
                        // these are less common, and might not be worth checking
                        return true;
                }
        }
        return false;
}

?>

使用方法:

<?php
$ismobile = check_user_agent('mobile');
if($ismobile) {
return 'yes';
} else {
return 'no';
}
?>

关于php - 检测手机浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6524301/

相关文章:

ios - 如何在不同地理位置模拟原生 iOS 应用并执行延迟测试

android - 使用双 SIM 卡功能时如何获取 PhoneStateListener

php - PHP 的 7z 扩展?

PHP 脚本无法正确上传图片

php - 在 laravel 中搜索 undefined variable

php - 如何在Windows中使用命令行在服务器上编译iPhone应用程序源代码?

java - 如何从应用程序 (Android) 中禁用/启用移动数据

javascript - 检测浏览器 TLS 兼容性

ios - 如何检查 ios url 连接中是否发生超时?

vbscript - 检测触摸板的坐标