[英]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. 这不是万无一失,但它是一个开始。
@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.