【问题标题】:How to get user device / Android /Ios/ Web [duplicate]如何获取用户设备/Android /Ios/Web [重复]
【发布时间】:2016-07-06 04:31:12
【问题描述】:

我在这里询问有关获取用户设备(即 Android、iOS 或 Web)的脚本。 我尝试过类似但在 android 手机中显示 Linux。

$agent = $_SERVER["HTTP_USER_AGENT"];
//print_r($agent);

if (preg_match('/Linux/', $agent))
$os = 'Linux';
elseif (preg_match('/Windows/', $agent))
$os = 'Windows';
elseif (preg_match('/Mac/', $agent))
$os = 'Mac';
elseif (preg_match('/Android/', $agent))
$os = 'Android';
elseif (preg_match('/Apple/', $agent))
$os = 'Apple';
else
$os = 'UnKnown';

echo $os;

【问题讨论】:

  • 因为安卓有linux内核,所以应该显示Linux
  • 只需更改ifs 的顺序:首先检查更具体的值(例如“Android”),然后检查“Linux”。

标签: php regex


【解决方案1】:

正如我在评论中所说,Android 有一个 linux 内核,所以要再次查找操作系统是否专门为 Android,请执行以下操作:

$agent = $_SERVER["HTTP_USER_AGENT"];
//print_r($agent);

if (preg_match('/Linux/', $agent))
{
    $os = 'Linux';
    if (preg_match('/Android/', $agent))
    {
        $os = 'Android';
    }
}
elseif (preg_match('/Windows/', $agent))
{
    $os = 'Windows';
}
elseif (preg_match('/Mac/', $agent))
{
    $os = 'Mac';
}
elseif (preg_match('/Apple/', $agent))
{
    $os = 'Apple';
}
else
{
    $os = 'UnKnown';
}

echo $os;

它的作用是,当它得到(操作系统)是 linux 时,它还会检查操作系统是否也是 android。

在您使用的是,每当它发现(操作系统)是 linux 时,它会将值 linux 放入 $os 并退出所有那些 elseif 的字符串。所以无法进入elseif (preg_match('/Windows/', $agent)) {...}

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 2018-07-15
    • 2012-09-30
    • 2014-11-30
    • 1970-01-01
    • 2019-01-25
    • 2011-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多