返回列表 发帖

[技术文章] 帝国cms中判断用户是否是手机访问方法

随着移动设备的普及,网站也会迎来越来越多移动设备的访问。用适应PC的页面,很多时候对手机用户不友好,那么有些时候,我们需要判断用户是否用手机访问,如果是手机的话,就跳转到指定的手机友好页面。如何在帝国cms模板中进行判断呢?这里就介绍一下,如何判断用户是否用手机访问。

方法如下:
将以下代码 放在 e/class/userfun.php 里
  1. function check_wap(){
  2.     // 先检查是否为wap代理,准确度高
  3.     if(stristr($_SERVER['HTTP_VIA'],"wap")){
  4.         return true;
  5.     }
  6.     // 检查浏览器是否接受 WML.
  7.     elseif(strpos(strtoupper($_SERVER['HTTP_ACCEPT']),"VND.WAP.WML") > 0){
  8.         return true;
  9.    }
  10.    //检查USER_AGENT
  11.    elseif(preg_match('/(blackberry|configuration\/cldc|hp |hp-|htc |htc_|htc-|iemobile|kindle|midp|mmp|motorola|mobile|nokia|opera mini|opera |Googlebot-Mobile|YahooSeeker\/M1A1-R2D2|android|iphone|ipod|mobi|palm|palmos|pocket|portalmmm|ppc;|smartphone|sonyericsson|sqh|spv|symbian|treo|up.browser|up.link|vodafone|windows ce|xda |xda_)/i', $_SERVER['HTTP_USER_AGENT'])){
  12.         return true;         
  13.     }
  14.     else{
  15.         return false;
  16.    }
复制代码
}
然后在需要判断跳转的页面加
  1. $agent = check_wap();
  2. if( $agent )
  3. {
  4.     header('Location: http://wap.52img.cn');
  5.     exit;
  6. }
复制代码
欢迎光临:逐梦论坛

判断手机用户,直接跳转到e/wap

回复 1# shillan


<script type="text/javascript">
var bForcepc = fGetQuery("dv") == "pc";
function fBrowserRedirect(){
        var sUserAgent = navigator.userAgent.toLowerCase();
        var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";        
        var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
        var bIsMidp = sUserAgent.match(/midp/i) == "midp";
        var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
        var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
        var bIsAndroid = sUserAgent.match(/android/i) == "android";
        var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
        var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
        if(bIsIpad){
                var sUrl = location.href;   
                if(!bForcepc){
                        window.location.href = "你的WAP地址";
                }
        }
        if(bIsIphoneOs || bIsAndroid){
                var sUrl = location.href;   
                if(!bForcepc){
                        window.location.href = "你的WAP地址";
                }
        }
        if(bIsMidp||bIsUc7||bIsUc||bIsCE||bIsWM){
                var sUrl = location.href;   
                if(!bForcepc){
                        window.location.href = "你的WAP地址";
                }
        }
}
function fGetQuery(name){//获取参数值
        var sUrl = window.location.search.substr(1);
        var r = sUrl.match(new RegExp("(^|&)" + name + "=([^&]*)(&|$)"));
        return (r == null ? null : unescape(r[2]));
}
function fShowVerBlock(){        
        if(bForcepc){
                document.getElementById("dv_block").style.display = "block";
        }
        else{
                document.getElementById("ad_block").style.display = "block";
        }
}
fBrowserRedirect();
</script>

这段代码放在<head>里,或者在footer里,随你便啦。个人建议放在head

各位高人也可以来改一下,变成那种只要用手机访问这个站点,就跳出是否用wap方式浏览的选择框。
欢迎光临:逐梦论坛

TOP

返回列表

Powered by Discuz! 7.2   论坛QQ群:逐梦论坛群

© 2001-2021 Comsenz Inc. 鲁公网安备 37120302000001号