PHP code to determine whether the access source is a mobile phone and automatically jump

WBOY
Release: 2016-07-25 08:57:28
Original
1195 people have browsed it
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;
    }
}
?>
Copy after login

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;
}
?>
Copy after login

For those who are interested, try both methods to see which one can accurately obtain the source of visitors.



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template