php 检测手机浏览器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6524301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:38:32  来源:igfitidea点击:

Detect mobile browser

phpmobiledetect

提问by Gromdroid

Possible Duplicate:
Simplest way to detect a mobile device

可能的重复:
检测移动设备的最简单方法

I have a site and I want to detect which browser is used and redirect them. I have a php index and the code must be in php. I've found many sites but they don't work or they don't detect many mobile browsers. Do you know of any good code or tutorials that can detect many mobile browsers?

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

回答by iamandrus

Have my user agent code:

有我的用户代理代码:

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

?>

How to use:

如何使用:

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

回答by Justin DoCanto

I wrote this script to detect a mobile browserin PHP.

我编写了这个脚本来检测PHP 中的移动浏览器

The code detects a user based on the user-agent string by preg_match()ing. It has 100% accuracy on all current mobile devices and I'm currently updating it to support more mobile devices as they come out. The code is called isMobile and is as follows:

该代码通过 preg_match()ing 根据用户代理字符串检测用户。它在所有当前的移动设备上都具有 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"]);
}

You can use it like this:

你可以这样使用它:

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

To redirect a user to your mobile site, I would do this:

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

// 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/");

Let me know if you have any questions and good luck!

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

回答by cweiske

At work, we use WURFL- there are millions of different browsers out there, and you're better of to re-use the work that others with experience did in that regard than implementing your own solution.

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