How to use PHP to determine whether a client is a mobile phone or a computer? In fact, developers know that when visiting a website through a browser , the browser sends a UA to the server, the user agent. UA for different browsers, different versions of the same browser, mobile browsers, and computer-side browsers are not the same. We just need to write the procedure to get the UA and then determine what the UA is, and then we can tell if it's a phone or a computer.
Below, the PHP code to determine whether the visitor's browser UA is a mobile browser, and then automatically jump to the corresponding page:
<?Phpfunction Is_mobile () {//Regular expression, batch with different mobile browser UA keywords. $regex _match= "/(Nokia|iphone|android|Motorola|^mot\-|softbank|foma|docomo|kddi|up\.browser|up\.link| "; $regex _match.= "htc|dopod|blazer|netfront|helio|hosin|huawei|novarra| Coolpad|webos|techfaith|palmsource| "; $regex _match.= "blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam\-|s[cg]h|^lge|ericsson| Philips|sagem|wellcom|bunjalloo|maui| "; $regex _match.= "Symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte\-| Longcos|pantech|gionee|^sie\-|portalmmm| "; $regex _match.= "jig\s browser|hiptop|^ucweb|^benq|haier|^lct|opera\s*mobi|opera\*mini|320x320| 240x320|176x220 "; $regex _match.=")/I "; return isset ($_ server[' Http_x_wap_profile ') or isset ($_server[' http_profile ']) or Preg_match ($regex _match, Strtolower ($_server[' Http_user_agent '])); If the above keyword exists in the UA, it returns true. } if (Is_mobile ()) { //If True, the WAP page. HeaDer ("http/1.1 301 Moved Permanently"); header ("location:http:// www.966266.com/wap.php "); }else { //If False, Then the Web page is made. header ("http/1.1 301 Moved Permanently"); header ("location:http://www.966266.com/"); }?>
Jump based on browser UA information (mobile and PC)