【问题标题】:Detect mobile browser [duplicate]检测移动浏览器[重复]
【发布时间】:2011-06-29 16:48:41
【问题描述】:

可能重复:
Simplest way to detect a mobile device

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

【问题讨论】:

标签: php mobile detect


【解决方案1】:

有我的用户代理代码:

<?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';
}
?>

【讨论】:

  • 嗨,非常感谢。这对我真的很有帮助。
  • 不错的实现。赞一个!
  • 这段代码仍然有效。但有些事情发生了变化。 Apple iPhone 现在将 Mozilla 置于他们的用户代理面前。因此,首先检查移动设备,然后检查桌面设备。
  • 在移动的行中,添加一个 |Mobile|在 android 上检测 firefox mobile。除了很棒的脚本,我终于找到了一个真正的 php 脚本,赞一个,赞!
  • 哦,如果某些设备无法正常工作,只需添加一个 echo $user_agent;找到它的名字。不知何故,我注意到脚本在无法检测到(firefox)时停止
【解决方案2】:

我用 PHP 写了this script to detect a mobile browser

代码通过 preg_match() 检测基于用户代理字符串的用户。它在所有当前的移动设备上都具有 100% 的准确性,我目前正在对其进行更新以支持更多的移动设备。代码叫做isMobile,如下:

function isMobile() {
    return preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"]);
}

你可以这样使用它:

// Use the function
if(isMobile())
    // Do something for only mobile users
else
    // Do something for only desktop users

要将用户重定向到您的移动网站,我会这样做:

// Create the function, so you can use it
function isMobile() {
    return preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"]);
}
// If the user is on a mobile device, redirect them
if(isMobile())
    header("Location: http://m.yoursite.com/");

如果您有任何问题,请告诉我,祝您好运!

【讨论】:

  • @AndrewBarber:似乎它满足了here 列出的所有要求
  • @robert 好的。我的问题是答案几乎是重复的,但我明白:)
  • 我不明白问题是什么。多个问题询问如何在 PHP 中重定向,所以我回答了。你们会建议我这样做吗?
  • 这个脚本对我的 IE8 造成了问题。它在用户代理中返回“Tablet PC 2.0”,并且此脚本将他检测为移动设备错误
  • 死链接。请更新。
【解决方案3】:

在工作中,我们使用WURFL - 有数百万种不同的浏览器,您最好重复使用其他有经验的人在这方面所做的工作,而不是实施您自己的解决方案。

【讨论】:

  • 可能有助于展示或链接到在 PHP 中使用 WURFL 来完成 @Gromdroid 想要的示例。
猜你喜欢
  • 1970-01-01
  • 2011-10-01
  • 2019-10-09
  • 1970-01-01
  • 2011-04-18
  • 2015-09-26
  • 1970-01-01
  • 1970-01-01
  • 2012-04-06
相关资源
最近更新 更多