PHP: using HTTP_USER_AGENT to determine whether it is a mobile phone function code _ PHP Tutorial

Source: Internet
Author: User
In PHP, HTTP_USER_AGENT is used to determine whether it is the function code of the mobile terminal. Sometimes it is very useful in some scenarios. keep the slave copy code as follows: functionis_mobile_request () {$ _ SERVER [ALL_HTTP] isset ($ _ SERVER [ALL_HTTP])? $ _ SERVER [ALL_HTT is sometimes useful in some scenarios. keep it for backup.

The code is as follows:


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 '], 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 ++;
// 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;
}


Source: http://yi1.com.cn/posts/781

Php determines the browser type based on HTTP_USER_AGENT

The code is as follows:


Function browsers (){
Global $ HTTP_USER_AGENT;
If (isset ($ HTTP_USER_AGENT )){
$ SAgent = $ HTTP_USER_AGENT;
} Else {
$ SAgent = $ _ SERVER ['http _ USER_AGENT '];
}
If (strpos ($ sAgent, 'msi ')! ==False & strpos ($ sAgent, 'Mac') ===false & strpos ($ sAgent, 'Opera ') === false ){
$ IVersion = (float) substr ($ sAgent, strpos ($ sAgent, 'msie ') + 5, 3 );
Return ($ iVersion> = 5.5 );
} Else if (strpos ($ sAgent, 'Gecko /')! = False ){
$ IVersion = (int) substr ($ sAgent, strpos ($ sAgent, 'Gecko/') + 6, 8 );
Return ($ iVersion> = 20030210 );
} Else {
Return false;
}
}

The authorization code is as follows: function is_mobile_request () {$ _ SERVER ['all _ http'] = isset ($ _ SERVER ['all _ http'])? $ _ SERVER ['all _ HTT...

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.