php或js判断网站访问者来自手机或者pc

php或js判断网站访问者来自手机或者pc机

       2013年9月26日,在弄wtuonline的时候为了区分用户是来自手机版浏览器还是pc,针对不同平台选择不同的网站版本,最终总结如下:

        一、JS版代码:

<!--切换手机版网站--->
  <script src="http://siteapp.baidu.com/static/webappservice/uaredirect.js" type="text/javascript"></script>
  <script type="text/javascript">uaredirect("http://www.baidu.com/");</script>

    //Js方法二

<script type="text/javascript">
            <!-- //平台、设备和操作系统
            var system ={
                win : false,
                mac : false,
                xll : false
            };
            //检测平台
            var p = navigator.platform;
            system.win = p.indexOf("Win") == 0;
            system.mac = p.indexOf("Mac") == 0;
            system.x11 = (p == "X11") || (p.indexOf("Linux") == 0);
            //跳转语句,如果是手机访问就自动跳转到wap.baidu.com页面
            if(system.win||system.mac||system.xll){
                window.location.href="http://www.php186.com";
            }else{
                window.location.href="http://wap.php186.com";
            }
            -->
        </script>

        二、PHP代码版:

<?php

        //判断手机还是电脑访问网站方法一:
        function isMobile() {
            if (isset ($_SERVER['HTTP_X_WAP_PROFILE'])) 
            {
                echo "移动设备";
            }else{
                echo "PC机";
            }
        }

         //判断手机还是电脑访问网站方法二:
       

  if (isset ($_SERVER['HTTP_VIA'])) 
         {
          //找不到为flase,否则为true
          if(stristr($_SERVER['HTTP_VIA'], "wap"))
          {
            echo "移动设备";
          }else{
            echo "PC机";
          }
         }

          //判断手机还是电脑访问网站方法三:
        

 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',
           'operamini',
           'operamobi',
           'openwave',
           'nexusone',
           'cldc',
           'midp',
           'wap',
           'mobile'
          );
          // 从HTTP_USER_AGENT中查找手机浏览器的关键字
          if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) 
          {
            echo "移动设备";
          }else{
            echo "PC机";
          }
         }
         
        ?>

  //方法四:综合以上

<?php

$mobile_browser = '0';

if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone)/i', strtolower($_SERVER['HTTP_USER_AGENT']))) {
$mobile_browser++;
}

if((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml')>0) or ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))) {
$mobile_browser++;
}

$mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,4));
$mobile_agents = array(
'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
'newt','noki','oper','palm','pana','pant','phil','play','port','prox',
'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
'wapr','webc','winw','winw','xda','xda-','Googlebot-Mobile');

if(in_array($mobile_ua,$mobile_agents)) {
$mobile_browser++;
}

if (strpos(strtolower($_SERVER['ALL_HTTP']),'OperaMini')>0) {
$mobile_browser++;
}

if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'windows')>0) {
$mobile_browser=0;
}

if($mobile_browser>0) {
header("Location: mobile.php"); //手機版
}else {
header("Location: pc.php"); //電腦版
}

 

下面有为网友总结的前三种方法和优劣分析:

http://www.imiku.com/html/2010/11/165.html

 

我的最终解决方案选择方法四,综合了前面三种:

pc版的主页:http://domain/index.php

index.php代码:

<?php 

    /**判断是否手机浏览器
     * @return boolean
     */
    function is_moble(){
        $mobile_browser = '0';
        
        if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone)/i', strtolower($_SERVER['HTTP_USER_AGENT']))) {
            $mobile_browser++;
        }
        if((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml')>0) or ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))) {
            $mobile_browser++;
        }
        $mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,4));
        $mobile_agents = array(
                'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
                'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
                'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
                'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
                'newt','noki','oper','palm','pana','pant','phil','play','port','prox',
                'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
                'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
                'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
                'wapr','webc','winw','winw','xda','xda-','Googlebot-Mobile');
        
        if(in_array($mobile_ua,$mobile_agents)) {
            $mobile_browser++;
        }
        
        if (strpos(strtolower($_SERVER['ALL_HTTP']),'OperaMini')>0) {
            $mobile_browser++;
        }
        
        if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'windows')>0) {
            $mobile_browser=0;
        }
        
        if($mobile_browser>0) {
            return true;
            //header("Location: mobile.php"); //手機版
        }else {
            return false;
            //header("Location: pc.php");  //電腦版
        }
    }

    if(is_moble()){
        header("Location: http://m.wtuonline.cn/"); //手機版
        exit();
    }        
?>

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>武纺大在线</title>
</head>
<body>

pc版主页。。。。

</body>
</html>

 

posted @ 2013-09-26 20:33  Nob  阅读(1576)  评论(0编辑  收藏  举报