How can I define Android phones and Android tablets differently using the user agent header?

For my site, I need to know the difference between visiting an Android tablet and visiting an Android phone. It must be detected before the page is sent to the user, so using JavaScript to check screen resolution is not an option.

I am currently using this to detect an Android device: stripos ($ ua, 'android')

Is there anything unique about the tablet in this user agent?

+6
android php header user-agent
source share
4 answers

You can use PHP $ _SERVER ['HTTP_USER_AGENT'] and then case-insensitive eregi functions to look for the following, which assumes that the browser developer has followed Android's recommendations for user agent specifications:

$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 is not perfect, but it is the beginning.

+5
source share

Checkout the WURFL project. He should be able to help you not only on Android Phone and Android Tablet, but also on other devices.

0
source share

@Patrick Kershner, your code only works for the Apple product family. Each Apple device sends its own UA, but this does not apply to Android devices. In fact, the author of this post needs to know if the Android device is a tablet or a phone without using Javascript. The code presented by user336828 is a great workaround for this, although not 100% reliable: some cloning devices / white label / low quality can send exactly the same UA string from both tablets and phones, but I think this should work with most well-known branded devices. I just tested this code on both an Android phone and a tablet, and it works.

0
source share
 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

-2
source share

All Articles