php如何判断http请求来自移动端?

WBOY
Release: 2016-06-06 20:08:00
Original
1131 people have browsed it

这是网上写的代码:
// 判断是否是手机

<code>function is_mobile()
{
    $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
    $mobileList = array('windows phone', 'mac os',  'iphone', 'android', 'ipad');
    foreach($mobileList as $value) {
        if(strpos($agent, $value) !== false) {
            return true;
        }
    }
    return false;</code>
Copy after login
Copy after login

mac os为什么成为了移动端的标识?不是应该是苹果电脑吗?

回复内容:

这是网上写的代码:
// 判断是否是手机

<code>function is_mobile()
{
    $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
    $mobileList = array('windows phone', 'mac os',  'iphone', 'android', 'ipad');
    foreach($mobileList as $value) {
        if(strpos($agent, $value) !== false) {
            return true;
        }
    }
    return false;</code>
Copy after login
Copy after login

mac os为什么成为了移动端的标识?不是应该是苹果电脑吗?

你可以用Mobile_Detect库判断,git上有下载

以前也找过类似的问题

给出的答案差不多都是这个判断这个常量

$_SERVER['HTTP_USER_AGENT']

这个个人觉得可以换种方式解决。如果不是特别变态的需求,非得要php来做这件事的话,可以考虑下js去做这件事。js去做这件事简单方便又快捷,总感觉后台去做这些判断怪怪的。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template