Detect mobile devices - and tablet devices

denlau picture denlau · Apr 2, 2012 · Viewed 62.4k times · Source

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.

Answer

Joe picture Joe · Apr 2, 2012

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