Detect mobile devices - and tablet devices
Asked Answered
I

4

22

I am currently looking at some code for PHP detection of mobiles, which is probably quite easy.

Now I just have got one problem - I want to make it possible to make unique view-files in my MVC-framework for tablets, mobiles and web pages. So I need to split the tablet from the rest of the mobile devices.

Currently this is the code, that I am using:

public function isMobile()
{   
    if(preg_match('/(alcatel|amoi|android|avantgo|blackberry|benq|cell|cricket|docomo|elaine|htc|iemobile|iphone|ipad|ipaq|ipod|j2me|java|midp|mini|mmp|mobi|motorola|nec-|nokia|palm|panasonic|philips|phone|sagem|sharp|sie-|smartphone|sony|symbian|t-mobile|telus|up\.browser|up\.link|vodafone|wap|webos|wireless|xda|xoom|zte)/i', $_SERVER['HTTP_USER_AGENT']))
    return true;
else
    return false;
}

But this is not enough - the only check is wheter the device is a mobile device - if not it is as standard telling the framework, that we're on a computer. The last part is ok - but I want to make a split of the mobile devices in actual mobiles - and in a secound group, which should be tablets.

I hope, that I have made my wish clear, and I hope, that you have some input in a good way to achieve this.

Thanks in advance.

Incubate answered 2/4, 2012 at 19:52 Comment(2)
So your regexp is not ok or you want to split this regexp in multiple (for mobile and tablets)?Canal
> Here is a class with methods for detecting each platform individually. > > code.google.com/p/php-mobile-detect That only work in opera on tablets. The builtin android browser on tablets returns its a mobile.Arnulfo
R
33

Here is a class with methods for detecting each platform individually.

Rupture answered 2/4, 2012 at 19:57 Comment(5)
This will actually answer the question - IF you make sure to make the isTablet-check before the isMobile-check.. Because the tablets is also marked as "mobiles" - for logic reasons.. :) Thank you, Joe!Incubate
Just found that earlier today! I was going to answer this question exactly how this answer answered it! +1Stockish
Anything like this available for ASP.NET?Epidermis
@greenPerson It looks as though this will give you similar functionality for ASP.Net.Rupture
Posted as an answer by hello: That only works in opera on tablets. The builtin android browser on tablets returns its a mobile.Semantic
P
8

Old question, but here goes my opinion regarding mobile detection.

You state in your question that you want unique files for mobile devices, so I can assume the reason for this is to present a different version of the website for mobile clients and desktop clients.

This approach is OK until a certain point. And that point is called Android. There are ~1.5 million Android devices activated each day with resolutions from 320*240 to 2560*1600, which makes it hard to if {} else for each of them. Even if you try to make a list with most used devices and you try to target only those, it will be hard to support a new device in the future.

My approach a while back was to forget about old method of splitting devices into "mobile" and "desktop" categories and create a new method. And that method consist in "good" and "bad" browsers which is based on browser capabilities. For example, if the browser supports local storage, it will be in "good" category.

Starting from this, I had the possibility to create a "base" version of the website, very basic from a UI standpoint but which will work cross-browser. This base version of the website will present the same content (because that matters at the end of the day) on all devices, will be very small in size (less assets, smaller html) and based on browser capabilities will be enriched on the client side.

So in the end you will end up with a website that has very small footprint (html size and assets), that looks OK cross browser and it will support any new device that comes up on the market without any changes, will load fast even on poor connections and that can be enriched on client side based on browser capabilities.

You can even enrich the webpage based on devices size: if the browser reports a large screen, you can bring in more assets, more ads and make the webpage more beautiful ; if the browser reports is on a small screen, you leave it as is.

Peaslee answered 18/2, 2014 at 10:10 Comment(2)
Actually this answer is worth reading no matter what - it is a cool approach, that I haven't ever thought about :)Incubate
Entirely approve of this approach, and it is exactly what I am trying to achieve, but it is certainly easier said than done, at least at a server side level!Emptyhanded
T
5

Mobile_Detect is a lightweight PHP class for detecting mobile devices (including tablets). It uses the User-Agent string combined with specific HTTP headers to detect the mobile environment. — Read more http://mobiledetect.net

Theretofore answered 10/6, 2013 at 11:18 Comment(0)
S
0

You can also use a more comprehensive solution like WURFL Cloud, which is a service that detects the capabilities of mobile devices, like is_mobile and is_tablet. There is a free plan for low traffic sites: http://www.scientiamobile.com/cloud

Se answered 7/4, 2012 at 17:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.