In the development of this technique is also commonly used by us, now using a mobile phone than the use of PC more convenient, so sometimes in order to make the system more friendly, to automatically determine whether it is mobile access, if it is to enter a different path. Today this article mainly combines several methods to synthesize a function, which is more convenient to use.
Here is the code implementation process:
<?php/** * ======================================= * Created by Zhihua_w. * Author:zhihua_w * DATE:2016/12/5 0006 * Time: Morning 8:45 * project:php Development Tips * Power: Determine if it is mobile Access * ================= ====================== *//** * To determine whether the current user is the PC side, or the phone side, return true to the phone end, false for the PC side * @return Boolean */function IsMobile () {//If there is http_x_wap_profile, it must be the mobile device if (Isset ($_server[' http_x_wap_profile ')) return true; If VIA information contains WAP, it must be a mobile device, some service associations block the information if (Isset ($_server[' Http_via ')) {//Cannot find for flase, otherwise true return Stristr ($_server[' Http_via '), "WAP")? True:false; }//Brain residue method, determine the mobile phone sent by the client flag, 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 ', ' Operami Ni ', 'Operamobi ', ' Openwave ', ' nexusone ', ' cldc ', ' MIDP ', ' wap ', ' mobile '; Look for the keyword if (Preg_match ("/(") of the phone's browser from Http_user_agent. Implode (' | ', $clientkeywords). ")/I", Strtolower ($_server[' http_user_agent '))) return true; }//protocol method, because it may not be accurate, put to the Last Judgment if (Isset ($_server[' http_accept ')) {//If only WML is supported and HTML is not supported that must be a mobile device/ /if WML and HTML are supported but WML before HTML is the mobile device if ((Strpos ($_server[' http_accept '), ' VND.WAP.WML ')!== false) && (Strpo S ($_server[' http_accept '), ' text/html ') = = = False | | (Strpos ($_server[' http_accept '), ' vnd.wap.wml ') < Strpos ($_server[' http_accept '], ' text/html ')))) {return true; }} return false; }?>