Heim  >  Artikel  >  Backend-Entwicklung  >  php如何判断http请求来自移动端?

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

WBOY
WBOYOriginal
2016-06-06 20:08:001101Durchsuche

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

<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去做这件事简单方便又快捷,总感觉后台去做这些判断怪怪的。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn