Php website determine whether a user is accessed by a mobile phone _ PHP Tutorial

Source: Internet
Author: User
Php website determines whether a user is accessed by a mobile phone. With the popularization of mobile devices, more and more mobile devices will access websites. Pages adapted to PCs are often unfriendly to mobile users. in some cases, we need to determine that 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 users are accessed by mobile phones. if they are mobile phones, they will jump to the specified mobile phone 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: http://www.nowamagic.net ');
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 the PHP framework is separated from the mobile terminal: (tested, comprehensive, and recommended)

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;

Bytes. Pages adapted to PCs are often unfriendly to mobile phone users, so sometimes we need to determine...

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.