简体   繁体   English

如何使用用户代理标头以不同方式检测Android手机和Android平板电脑?

[英]How can I detect Android phones and Android tablets differently using the user agent header?

For my site I need to be able to tell the difference between when an Android tablet visits and when an Android phone visits. 对于我的网站,我需要能够区分Android平板电脑访问时间和Android手机访问时间。 It needs to be detected before the page is sent to the user so using JavaScript to check the screen res isn't an option. 在将页面发送给用户之前需要检测它,因此使用JavaScript检查屏幕res不是一个选项。

At the moment I use this to detect an android device: stripos($ua, 'android') 目前我用它来检测一个Android设备:stripos($ ua,'android')

Is there anything unique thar a tablet has in it's user agent? 平板电脑在用户代理中有什么独特之处吗?

You can use PHP's $_SERVER['HTTP_USER_AGENT'] then case-insensitive eregi functions to look for the following, which assumes the browser developer followed Android's guidelines on user agent specification: 您可以使用PHP的$ _SERVER ['HTTP_USER_AGENT']然后不区分大小写的eregi函数来查找以下内容,假设浏览器开发人员遵循Android的用户代理规范指南:

$ua = $_SERVER['HTTP_USER_AGENT'];
if (eregi('Android', $ua) && eregi('Mobile', $ua)) $platform = "Android Phone";
elseif (eregi('Android', $ua) && !eregi('Mobile', $ua)) $platform = "Android Tablet";

It's not foolproof but it's a start. 这不是万无一失,但它是一个开始。

Checkout the WURFL project. 查看WURFL项目。 It should be able to help you out not only in Android Phone and Android Tablet scenario but also other devices as well. 它应该能够帮助您不仅在Android手机和Android平板电脑方案,而且还有其他设备。

@Patrick Kershner, your code only works for the family of Apple products. @Patrick Kershner,您的代码仅适用于Apple产品系列。 Each Apple device sends its own UA, but that's not the case with Android devices. 每个Apple设备都会发送自己的UA,但Android设备并非如此。 In fact, the author of this post needs to know whether the Android device is a tablet or a phone without using Javascript. 事实上,这篇文章的作者需要知道Android设备是平板电脑还是手机而不使用Javascript。 The code submitted by user user336828 is a great workaround for this, although not 100% reliable: some clone/white-label/low quality devices may send exactly the same UA string from both tablets and phones, but I think this should work with most known brand's devices. 用户user336828提交的代码是一个很好的解决方法,虽然不是100%可靠:一些克隆/白标/低质量设备可能从平板电脑和手机发送完全相同的UA字符串,但我认为这应该适用于大多数知名品牌的设备。 I just tested that code on both an Android phone and a tablet and it's working. 我刚刚在Android手机和平板电脑上测试了该代码,它正在运行。

if(strstr($_SERVER['HTTP_USER_AGENT'],'iPod') ||   strstr($_SERVER['HTTP_USER_AGENT'],'iPhone') || strstr($_SERVER['HTTP_USER_AGENT'],'iPad') || strstr($_SERVER['HTTP_USER_AGENT'],'Android')){ 
    //do something...
}

Worked for me 为我工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检测使用PHP打开的邮件是否已打开,在哪个浏览器(用户代理)和OS中打开? - How can i detect if the mail which i sent has been opened, and in which browser (user agent), and OS using PHP? 当我的用户代理不包含类似的东西时,我如何使用 php 检测用户使用 ipad - How can I detect with php that the user uses an ipad when my user agent doesnt conatin something like that 当用户代理来自WebView Android应用程序时如何隐藏网站标题(div)? - How to hide website header (div) when user agent comes from webview android app? 如何使用PHP检测用户代理支持的HTTP版本? - How do I detect what version of HTTP a user-agent supports, using PHP? 如何使用PHP检测“Google Chrome”作为用户代理? - How to detect “Google Chrome” as the user-agent using PHP? 无法删除标头“变化:用户代理” - Can not remove header “vary: user agent” 如何检测iPhone或iPad的用户代理? - How to detect user agent of iphone or ipad? 如何通过PHP推送到iPhone / Android手机 - How to push to iphone / android phones via php 使用Requests for PHP库,如何设置自定义用户代理? - Using the Requests for PHP library, how can I set a custom user-agent? 如何使用“ HTTP_USER_AGENT”仅显示浏览器名称和版本而没有额外信息? - How can I just display the browser name and version only no extra information using' HTTP_USER_AGENT'?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM