Detect mobile devices - and tablet devices

62,581

Solution 1

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

Solution 2

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.

Solution 3

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

Share:
62,581

Related videos on Youtube

denlau
Author by

denlau

Updated on July 09, 2022

Comments

  • denlau
    denlau almost 2 years

    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.

    • tonymarschall
      tonymarschall about 12 years
      So your regexp is not ok or you want to split this regexp in multiple (for mobile and tablets)?
    • hello
      hello about 12 years
      > 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.
  • denlau
    denlau about 12 years
    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!
  • pattyd
    pattyd almost 11 years
    Just found that earlier today! I was going to answer this question exactly how this answer answered it! +1
  • Sergey
    Sergey over 10 years
    Anything like this available for ASP.NET?
  • Joe
    Joe over 10 years
    @greenPerson It looks as though this will give you similar functionality for ASP.Net.
  • Petr R.
    Petr R. over 10 years
    Posted as an answer by hello: That only works in opera on tablets. The builtin android browser on tablets returns its a mobile.
  • deathlock
    deathlock over 10 years
    Thanks, it's pretty useful.
  • denlau
    denlau about 10 years
    Actually this answer is worth reading no matter what - it is a cool approach, that I haven't ever thought about :)
  • Jamie Hartnoll
    Jamie Hartnoll over 9 years
    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!