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

作者: 龙哥 分类: 技术 发布时间: 2015-02-06 12:18 ė1,853 次浏览

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

 

        一、JS版代码:

<!--切换手机版网站--->
  <script src="http://siteapp.baidu.com/static/webappservice/uaredirect.js" type="text/javascript"></script>
  <script type="text/javascript">uaredirect("http://www.jlwz.cn/");</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);
            //跳转语句,如果是手机访问就自动跳转到jlwz.cn页面
            if(system.win||system.mac||system.xll){
                window.location.href="http://www.jlwz.cn";
            }else{
                window.location.href="http://jlwz.cn";
            }
            -->
        </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"); //電腦版
}

本文出自 俞凌龙博客,转载时请注明出处及相应链接。

本文永久链接: https://blog.jlwz.cn/155

Ɣ回顶部