Determine the client type based on php and php determine the client type

Source: Internet
Author: User

Determine the client type based on php and php determine the client type

During statistics on the number of website users, we often need to obtain the types of user browsers and operating systems. This article mainly introduces how to determine the client type based on php, if you need it, you can find out.

Method 1:

/***** Determine whether a browser belongs to a PC or a mobile terminal based on the strings specified by each browser contained in php $ _ SERVER ['HTTP _ USER_AGENT *@ author discuz3x * @ lastmodify 2014-04-09 * @ return BOOL */function checkmobile () {global $ _ G; $ mobile = array (); // The String array static $ touchbrowser_list = array ('iphone ', 'android', 'phone ', 'mobile', 'wap ', 'netfront', 'java', 'Opera mobi', 'Opera mini ', 'ucweb', 'windows ce', and 'symbian ', 'Series', 'webos', 'sony ', 'blackberry', 'dopod ', 'nokia', 'samsung ', 'palmsource', 'xda ', 'pieplus ', 'meizu', 'midp', 'cldc ', 'motorola', 'fom', 'docomo', 'Up. browser ', 'Up. link ', 'blazer', 'helio', 'hosin ', 'huawei', 'novara', 'coolpad ', 'webos', 'techfaith ', 'palmsource ', 'alcatel', 'amodi', 'ktouch', 'nexian ', 'ericsson', 'philips ', 'sagem', 'wellcom', 'bunjalloo', 'maui ', 'smartphone', 'I Emobile ', 'spice', 'bird ', 'zte-', 'longcos ', 'pantech', 'gionee ', 'portalmmm', 'jig browser ', 'hiptop', 'benq', 'haier ',' ^ lct ', '320x320', '240x320', '176x220 '); // window mobile browser array [guess] static $ mobilebrowser_list = array ('windows phone '); // static $ wmlbrowser_list = array ('cect ', 'compal', 'ctl ', 'lg ', 'nec', 'tcl ', 'alcatel', 'ericsson', 'bird ', 'daxian', 'd Btel ', 'eastcom', 'pantech', 'dopod ', 'philips', 'haier ', 'konka', 'kejian ', 'lenovo', 'benq ', 'mot', 'soutec ', 'nokia', 'sagem ', 'sgh', 'sed', 'capitel ', 'panasonic', 'sonyericsson ', 'sharp ', 'amodi', 'panda', 'zte'); $ pad_list = array ('pad', 'gt-p1000 '); $ useragent = strtolower ($ _ SERVER ['HTTP _ USER_AGENT ']); if (dstrpos ($ useragent, $ pad_list) {return false ;} if ($ v = dstrpos ($ useragent, $ mobileb Rowser_list, true) {$ _ G ['mobile'] = $ v; return '1';} if ($ v = dstrpos ($ useragent, $ touchbrowser_list, true) {$ _ G ['mobile'] = $ v; return '2';} if ($ v = dstrpos ($ useragent, $ wmlbrowser_list ))) {$ _ G ['mobile'] = $ v; return '3'; // wml} $ brower = array ('mozilla ', 'chromi', 'safari ', 'Opera ', 'mate', 'winwap', 'openwave ', 'myop'); if (dstrpos ($ useragent, $ brower) return false; $ _ G ['mobile'] = 'Unknown '; // for unknown browsers, use the $ _ GET ['mobile'] parameter to determine whether it is a mobile browser if (isset ($ _ G ['letletpl'] [$ _ GET ['mobile']). {return true;} else {return false ;}} /*** determine whether the element strings in $ arr appear in $ string * @ param $ string $ _ SERVER ['HTTP _ USER_AGENT '] * @ param $ arr browsers $ _ SERVER ['HTTP _ USER_AGENT '] must contain a string * @ param $ returnvalue to return the browser name or Boolean value, true indicates the browser name. false indicates the Boolean value. [Default] * @ author discuz3x * @ lastmodify 2014-04-09 */function dstrp OS ($ string, $ arr, $ returnvalue = false) {if (empty ($ string) return false; foreach (array) $ arr as $ v) {if (strpos ($ string, $ v )! = False) {$ return = $ returnvalue? $ V: true; return $ return;} return false ;}

Method 2:

<? Phpfunction isMobile () {// if HTTP_X_WAP_PROFILE exists, it must be a mobile device if (isset ($ _ SERVER ['HTTP _ X_WAP_PROFILE ']) {return true ;} // if the via information contains wap, it must be a mobile device. Some service providers will block this information if (isset ($ _ SERVER ['HTTP _ Ve']) {// cannot be found as flase; otherwise, it is true return stristr ($ _ SERVER ['HTTP _ vean'], "wap ")? True: false;} // identify the client flag sent by the mobile phone. The compatibility needs to be improved if (isset ($ _ SERVER ['HTTP _ USER_AGENT ']) {$ clientkeywords = array ('nokia ', 'sony', 'ericsson ', 'mot', 'samsung', 'htc ', 'sgh', 'lg ', 'sharp ', 'sie-', 'philips ', 'panasonic', 'alcatel', 'lenovo ', 'iphone', 'ipod ', 'blackberry ', 'meizu', 'android', 'netfront', 'symbian ', 'ucweb', 'windowsce', 'palm ', 'operamini', 'operamobi', 'openwave ', 'nexusone', 'cld C ', 'midp', 'wap ', 'mobile'); // search for the keyword if (preg_match ("/(". implode ('|', $ clientkeywords ). ")/I", strtolower ($ _ SERVER ['HTTP _ USER_AGENT ']) {return true ;}// resolution method, because it may be inaccurate, put it at the end to determine if (isset ($ _ SERVER ['HTTP _ ACCEPT ']) {// if only wml is supported and html is not supported, it must be a mobile device // if wml and html are supported, but wml is a mobile device before html (strpos ($ _ SERVER ['HTTP _ ACCEPT '], 'vnd. wap. wml ')! = False) & (strpos ($ _ SERVER ['HTTP _ ACCEPT '], 'text/html ') === false | (strpos ($ _ SERVER ['HTTP _ ACCEPT '], 'vnd. wap. wml ') <strpos ($ _ SERVER ['HTTP _ ACCEPT'], 'text/html ') {return true ;}} return false ;}?>

Determine whether the mobile phone is IOS or Android

The Code is as follows:

/*** Determine whether the mobile phone accesses */function is_mobile () {static $ is_mobile; if (isset ($ is_mobile) return $ is_mobile; if (empty ($ _ SERVER ['HTTP _ USER_AGENT ']) {$ is_mobile = false;} elseif (strpos ($ _ SERVER ['HTTP _ USER_AGENT'], 'mobile ')! = False // your mobile devices (all iPhone, iPad, etc.) | strpos ($ _ SERVER ['HTTP _ USER_AGENT '], 'android ')! = False | strpos ($ _ SERVER ['HTTP _ USER_AGENT '], 'silk /')! = False | strpos ($ _ SERVER ['HTTP _ USER_AGENT '], 'kindle ')! = False | strpos ($ _ SERVER ['HTTP _ USER_AGENT '], 'blackberry ')! = False | strpos ($ _ SERVER ['HTTP _ USER_AGENT '], 'Opera Mini ')! = False | strpos ($ _ SERVER ['HTTP _ USER_AGENT '], 'Opera Mobi ')! = False) {$ is_mobile = true;} else {$ is_mobile = false;} return $ is_mobile ;}

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

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.