TP3.2.x判断手机端访问,同一个域名在PC和手机端展示不同模板(半独立式网站)

首先介绍APP_STATUS内置常量,TP入口文件增加APP_STATUS 参数,  自动加载不同的项目配置文件,通过配置文件指向不同的模块

手机端访问时调用Wap手机模块,实现在手机端访问时展示出手机网站,无需跳转域名
首先我们在./Application/Common/Conf/ 目录下建立两个公共配置文件:config.php 和Mobie.php

config.php文件中 

<?php
return
array( 'DEFAULT_MODULE'=>'Index', 'DEFAULT_CONTROLLER'=>'Index', 'DEFAULT_ACTION'=>'index', );

Mobie.php文件:

<?php
return
array( 'DEFAULT_MODULE'=>'Mobie', 'DEFAULT_CONTROLLER'=>'Index', 'DEFAULT_ACTION'=>'index', );

  关键在单入口index文件中添加手机端判断方法(方法建议放到最下面),在文件中的21行后面也就是“定义应用目录”结束后,加入代码:

if(is_mobile_request() ){
    define('APP_STATUS','config_wap');
}else{
    define('APP_STATUS','Index');
}

  这样,就用关键的APP_STATUS来调用不同的模块,is_mobile_request()这样写:

function is_mobile_request(){
    $_SERVER['ALL_HTTP'] = isset($_SERVER['ALL_HTTP']) ? $_SERVER['ALL_HTTP'] : '';
    $mobile_browser = '0';
    if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|iphone|ipad|ipod|android|xoom)/i', strtolower($_SERVER['HTTP_USER_AGENT'])))
        $mobile_browser++;
    if((isset($_SERVER['HTTP_ACCEPT'])) and (strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml') !== false))
        $mobile_browser++;
    if(isset($_SERVER['HTTP_X_WAP_PROFILE']))
        $mobile_browser++;
    if(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-'
        );
     if(in_array($mobile_ua, $mobile_agents))
          $mobile_browser++;
     if(strpos(strtolower($_SERVER['ALL_HTTP']), 'operamini') !== false)
          $mobile_browser++;
     // Pre-final check to reset everything if the user is on Windows  
     if(strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'windows') !== false)
          $mobile_browser=0;
     // But WP7 is also Windows, with a slightly different characteristic  
     if(strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'windows phone') !== false)
          $mobile_browser++;
     if($mobile_browser>0)  
          return true;
     else
          return false;
}

  参考TP文档:http://document.thinkphp.cn/manual_3_2.html#load_config

 

  我还查到用JS判断的,没去亲自验证,代码如下:

  


(function(){ var res = GetRequest(); var par = res['index']; if(par!='gfan'){ var ua=navigator.userAgent.toLowerCase(); var contains=function (a, b){ if(a.indexOf(b)!=-1){return true;} }; var toMobileVertion = function(){ window.location.href = '手机站地址' } if(contains(ua,"ipad")||(contains(ua,"rv:1.2.3.4"))||(contains(ua,"0.0.0.0"))||(contains(ua,"8.0.552.237"))){return false} if((contains(ua,"android") && contains(ua,"mobile"))||(contains(ua,"android") && contains(ua,"mozilla")) ||(contains(ua,"android") && contains(ua,"opera")) ||contains(ua,"ucweb7")||contains(ua,"iphone")){toMobileVertion();} } })(); function GetRequest() { var url = location.search; //获取url中"?"符后的字串 var theRequest = new Object(); if (url.indexOf("?") != -1) { var str = url.substr(1); strs = str.split("&"); for(var i = 0; i < strs.length; i ++) { theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]); } } return theRequest; }

 

posted @ 2019-07-01 21:04  庹明志的IT博客  阅读(844)  评论(0编辑  收藏  举报