PHP checks whether the client is accessed by mobile phone based on HTTP_USER_AGENT.

Source: Internet
Author: User
The code is as follows: Copy code

 

// Judge moblie
Function is_mobile ()
{
$ _ 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 | ios)/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 '], 0, 4 ));
$ Mobile_agents = array (
'W3c ', 'ACS-', 'alav', 'alca', 'amodi', 'Audi', 'avany', 'benq', 'bird ', 'blac ',
'Blaz', 'brew', 'cell ', 'cldc', 'cmd-', 'Dang', 'Doc', 'Eric ', 'hipt ', 'inno ',
'Ipaq ', 'Java', 'glasis', 'dkdi', 'keji', 'Leno', 'LG-C', 'LG-D ', 'LG-G', 'lge -',
'Maui', 'maxo', 'midp ', 'mits', 'mmef', 'mobi', 'mot-', 'Moto', 'mwbp ', 'Nec -',
'Newt ', 'noki', 'login', 'Palm', 'pana ', 'pant', 'Phil', 'play', 'port', 'prox ',
'Qwap ', 'Sage', 'samples', '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 ++;
Www.111cn.net
// 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;
Else
Return false;
}

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.