【问题标题】:How to determine if a site is visited through mobile [duplicate]如何确定是否通过移动设备访问了网站[重复]
【发布时间】:2014-01-19 14:00:40
【问题描述】:

如果通过移动设备访问网站,我如何判断(在 php 中)? 我无法使用 get_browser() 因为我无法编辑 php.ini(设置 browscap.ini 的路径)。

【问题讨论】:

  • 在 SO 上有很多答案,例如 thisthis
  • 我不喜欢人们对问题投反对票。所以我投你一票!
  • 如果您想向移动用户显示不同的网站,请不要忘记现在流行的是使用响应式网站(您可以在 Google 上搜索)。如果你真的想通过用户代理进行检测,我建议你下载一个已经存在并且由其他人维护的代码。我在一些项目中使用了这个:detectmobilebrowsers.com 问题是世界上没有办法可以在所有移动设备上测试检测,因为有这么多。而且,当销售新的​​移动设备时,就会出现新的用户代理。

标签: php mobile browser


【解决方案1】:

如果是 PHP,你可以使用名为 Mobile Detect 的开源库

下载后,您可以将其包含在您的项目中,然后像这样使用它:

// Include and instantiate the class.
include 'Mobile_Detect.php';
$detect = new Mobile_Detect();

if ($detect->isMobile())
   // Any mobile device (phones or tablets).
else
   // desktop detected

另一种解决方案是通过检查user agent string来使用客户端技术,例如javasascript:

if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
    // mobile device detected
}

【讨论】:

    【解决方案2】:

    我正在使用来自this site 的代码。它支持所有现代语言,可以非常有效地检测浏览器和设备。

    【讨论】:

    • 别担心:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-23
    • 1970-01-01
    • 2018-08-05
    • 1970-01-01
    • 2012-12-17
    相关资源
    最近更新 更多