Maison  >  Article  >  développement back-end  >  php如何判断http请求来自移动端?

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

WBOY
WBOYoriginal
2016-06-06 20:08:001102parcourir

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

<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>

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>

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

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

以前也找过类似的问题

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

$_SERVER['HTTP_USER_AGENT']

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

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn