Home >Backend Development >PHP Tutorial >PHP code to determine whether the access source is a mobile phone and automatically jump
This article introduces two implementation methods for PHP to detect whether the access source is a mobile phone, and if so, it will automatically jump to the mobile phone page. Friends in need can refer to it.
The following two methods can both determine the terminal source of the visitor, whether it is a mobile phone or an ordinary web page, and then make a jump. Method 1: <?php $ua = strtolower($_SERVER['HTTP_USER_AGENT']); $uachar = "/(nokia|sony|ericsson|mot|samsung|sgh|lg|philips|panasonic| alcatel|lenovo|cldc|midp|mobile)/i"; if(($ua == '' || preg_match($uachar, $ua))&& !strpos(strtolower($_SERVER['REQUEST_URI']),'wap')) { $Loaction = 'mobile/'; if (!empty($Loaction)) { header("Location: $Loaction\n"); exit; } } ?> Method 2: <?php $is_wap = 0; if(strpos($_SERVER['HTTP_VIA'],"wap")>0){$is_wap = 1;} elseif(strpos(strtoupper($_SERVER['HTTP_ACCEPT']),"VND.WAP") > 0 || strpos(strtoupper($_SERVER['HTTP_ACCEPT']),"UC/") > 0){ $is_wap = 1;} else { $iUSER_AGENT=strtoupper(trim($_SERVER['HTTP_USER_AGENT'])); if(strpos($iUSER_AGENT,"NOKIA")>0 || strpos($iUSER_AGENT,"WAP")>0 || strpos($iUSER_AGENT,"MIDP")>0 || strpos($iUSER_AGENT,"UCWEB")>0 )$is_wap == 1; } if($is_wap==1){header('Location:wap/index.php');exit; } ?> For those who are interested, try both methods to see which one can accurately obtain the source of visitors. |