close

原文網址:http://www.dannyherran.com/2011/02/detect-mobile-browseruser-agent-with-php-ipad-iphone-blackberry-and-others/

以下原文轉載
----------------------------------------------------------------------------------------------

Detect mobile browser/user agent with PHP (iPad, iPhone, Android, BlackBerry, WP7 and others)

With all this load of smartphones and tablets, it is mandatory for us as developers to optimize our content as much as possible so everybody can see it, no matter what device they are using. This is why I wrote a small but powerful function to properly detect a wide range of mobile devices and redirect to an special content accordingly.

Lets take a look:

Code:
1
2
3
4
5
6
7
8
function detect_mobile()
{
    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;
}

This function can detect most (if not all) of all the standard “dumb” phones, smartphones, including the iPhone, iPad, Android and Windows Phone 7. Lets say you want to redirect your users to your blog if they are accessing your home page from a mobile device:

Code:
1
2
3
4
$mobile = detect_mobile();
 
if($mobile === true)
    header('Location: blog');

Nice and easy!

Changelog:
24/03/2011 – Updated for Android
25/04/2011 – Updated for Windows Phone 7
23/05/2011 – Updated for iPod Touch
16/01/2012 – Rewritten, shortened. Deleted unnecesary checks. Added Windows CE support.

 By Danny Herran · on February 2, 2011 at 6:52 pm · 63 comments

Tags: blackberry ipad iphone mobile nokia php samsung useragent

arrow
arrow
    全站熱搜

    yoonow 發表在 痞客邦 留言(0) 人氣()