Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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.

At the moment I use this to detect an android device: stripos($ua, 'android')

Is there anything unique thar a tablet has in it's user agent?

like image 796
Tom Avatar asked Oct 17 '10 07:10

Tom


People also ask

What does the user agent tell you?

The User-Agent (UA) string is contained in the HTTP headers and is intended to identify devices requesting online content. The User-Agent tells the server what the visiting device is (among many other things) and this information can be used to determine what content to return.

What is the user agent header?

The User-Agent request header is a characteristic string that lets servers and network peers identify the application, operating system, vendor, and/or version of the requesting user agent.

Is a user agent unique?

User-Agents also provides one of the data points for fingerprinting users without the use of cookies. It's included with every HTTP request and can potentially be very long and unique to a user in certain uncommon situations.


1 Answers

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:

$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.

like image 185
user336828 Avatar answered Nov 05 '22 08:11

user336828