PHP is used to determine whether a user accesses a mobile phone

Source: Internet
Author: User
This article describes how to use PHP to determine whether a user can access a mobile phone. It is very useful and recommended to friends who need it.

This article describes how to use PHP to determine whether a user can access a mobile phone. It is very useful and recommended to friends who need it.

With the popularization of mobile devices, more and more mobile devices will access websites. Pages adapted to PCs are often unfriendly to mobile phone users. In some cases, we need to determine whether or not users access the pages through mobile phones, to go to the specified mobile friendly page. Here we will introduce how to determine whether a user can access the website through a mobile phone.

The custom functions are as follows:

The Code is as follows:


$ Agent = check_wap ();
If ($ agent)
{
Header ('location :');
Exit;
}
// Check if wap
Function check_wap (){
// Check whether it is a wap proxy with High Accuracy
If (stristr ($ _ SERVER ['HTTP _ pass'], "wap ")){
Return true;
}
// Check whether the browser accepts WML.
Elseif (strpos (strtoupper ($ _ SERVER ['HTTP _ ACCEPT ']), "VND. WAP. WML")> 0 ){
Return true;
}
// Check USER_AGENT
Elseif (preg_match ('/(blackberry | configuration \/cldc | hp-| htc _ | htc-| iemobile | kindle | midp | mmp | motorola | mobile | nokia | opera mini | opera | Googlebot-Mobile | YahooSeeker \/M1A1-R2D2 | android | iphone | ipod | mobi | palm | palmos | pocket | portalmmm | ppc; | smartphone | sonyericsson | sqh | spv | symbian | treo | up. browser | up. link | vodafone | windows ce | xda _)/I ', $ _ SERVER ['HTTP _ USER_AGENT']) {
Return true;
}
Else {
Return false;
}
}

Another function is used to determine whether a mobile terminal is a mobile phone from the PHP framework:

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;
}

The code is very simple, the function is very practical, it is very suitable for putting it into the project, hope that friends can like it.

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.