Php determines whether a mobile phone is accessed or a computer browser

Source: Internet
Author: User

Method 1

<? Php function isMobile () {$ useragent = isset ($ _ SERVER ['HTTP_USER_AGENT'])? $ _ SERVER ['HTTP_USER_AGENT']: '; $ useragent_commentsblock = preg_match (' | (.*?) | ', $ Useragent, $ matches)> 0? $ Matches [0]: ''; function CheckSubstrs ($ substrs, $ text) {foreach ($ substrs as $ substr) if (false! = Strpos ($ text, $ substr) {return true;} return false;} $ mobile_ OS _list = array ('Google Wireless Transcoder', 'Windows CE', 'WindowsCE ′, 'symbian ', 'Android', 'armv6l', 'armv5', 'mobile ', 'centos', 'mobab', 'avantgo ', 'Opera mobi ′, 'j2'/MIDP ', 'Smartphone', 'Go. web ', 'Palm', 'ipaq '); $ mobile_token_list = array ('Profile/MIDP', 'configuration/CLDC-',' 160 × 160 ′, '176 × 220', '240 × 240', '240 × 320', '320 × 240', 'Up. browser ', 'Up. li Nk ', 'symbianos', 'palmos ', 'pocketpc', 'sonyericsson', 'Nokia ', 'BlackBerry', 'Vodafone', 'benq ′, 'novarra-Vision ', 'Iris', 'netfron', 'HTC _ ', 'xda _', 'SAMSUNG-SGH ', 'wapaka', 'docomo ', 'iPhone ′, 'iPod '); $ found_mobile = CheckSubstrs ($ mobile_ OS _list, $ useragent_commentsblock) | CheckSubstrs ($ mobile_token_list, $ useragent); if ($ found_mobile) {return true ;} else {return false ;}} if (isMobile () echo 'visit DD blog'; el Se echo 'computer access DD blog';?>

Method 2

Function is_mobile_request () {$ _ SERVER ['all_http '] = isset ($ _ SERVER ['all_http'])? $ _ SERVER ['all_http ']: ''; $ mobile_browser = '0'; if (preg_match ('/(up. browser | up. link | mmp | symbian | smartphone | midp | wap | phone | iphone | ipad | ipod | android | xoom)/I ′, strtolower ($ _ SERVER ['HTTP_USER_AGENT']) $ mobile_browser; if (isset ($ _ SERVER ['HTTP_ACCEPT']) and (strpos (strtolower ($ _ SERVER ['http_accept ']), 'application/vnd.wap.xhtml XML ′)! = False) $ mobile_browser; if (isset ($ _ SERVER ['HTTP_X_WAP_PROFILE']) $ mobile_browser; if (isset ($ _ SERVER ['HTTP_PROFILE']) $ mobile_browser; $ mobile_ua = strtolower (substr ($ _ SERVER ['http_user_agent '],); $ mobile_agents = array ('w3c', 'ACS -′, 'alav', 'alca', 'amo' I ', 'Audi', 'avance', 'benq', 'bird ', 'blac', 'blaz', 'brew ′, 'Cell ', 'cldc', 'cmd-', 'Dang ', 'doco', 'Eric', 'hipt', 'inno', 'ipaq ′, 'Java ', 'jeigs', 'kddi ', 'Keji', 'Leno', 'LG-C', 'LG-D', 'LG-G', 'lge-', 'mau ′, 'maxo', 'midp ', 'mits', 'mmef', 'mobi', 'mot-', 'Moto', 'mwbp ', 'Nec -′, 'Newt ', 'noki', 'rank', 'Palm ', 'pana', 'pant ', 'Phil', 'play', 'port', 'prox ′, 'qwap ', 'Sage', 'sams', 'sany', 'Sch-', 'SEC-', 'send', 'seri', 'sgh -′, 'Shar ', 'sie-', 'Siem ', 'smal', 'smar', 'Sony ', '7d-', 'symb', 'T-M ′, 'teli', 'Tim-', 'tosh', 'TSM-', 'upg1 ', 'upsi', 'vk-v', 'voda ′, 'wap-', 'wapa', 'wapi ', 'wapp ′, 'Wapr', 'webc', 'winw', 'winw', 'xda', 'xda-'); if (in_array ($ mobile_ua, $ mobile_agents) $ mobile_browser; if (strpos (strtolower ($ _ SERVER ['all_http ']), 'operamini ′)! = False) $ mobile_browser; // Pre-final check to reset everything if the user is on Windows if (strpos (strtolower ($ _ SERVER ['HTTP_USER_AGENT']), 'Windows ′)! = False) $ mobile_browser = 0; // But WP7 is also Windows, with a slightly different characteristic if (strpos (strtolower ($ _ SERVER ['HTTP_USER_AGENT']), 'Windows phone ′)! = False) $ mobile_browser; if ($ mobile_browser> 0) return true; elsereturn false ;}

I have tested both of these methods and hope they will be useful to you.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.