ThinkPHP3.2判断是否为手机端访问并跳转到另一个模块的步骤

WBOY
풀어 주다: 2016-06-13 12:23:56
원래의
1033명이 탐색했습니다.

ThinkPHP3.2判断是否为手机端访问并跳转到另一个模块的方法

<span style="color: #000000;">目录结构公共模块Common,Home模块,Mobile模块配置Application</span>/Common/Conf/<span style="color: #000000;">config.php文件</span><span style="color: #800000;">'</span><span style="color: #800000;">MODULE_ALLOW_LIST</span><span style="color: #800000;">'</span> =>  <span style="color: #800000;">'</span><span style="color: #800000;">Home,Mobile</span><span style="color: #800000;">'</span><span style="color: #000000;">接下来配置Application</span>/Common/Common/<span style="color: #000000;">function.php文件添加isMobile方法  【详见代码段1】function isMobile(){    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 如果有HTTP_X_WAP_PROFILE则一定是移动设备</span>    <span style="color: #0000ff;">if</span> (isset ($_SERVER[<span style="color: #800000;">'</span><span style="color: #800000;">HTTP_X_WAP_PROFILE</span><span style="color: #800000;">'</span><span style="color: #000000;">]))        </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;     </span><span style="color: #008000;">//</span><span style="color: #008000;">此条摘自TPM智能切换模板引擎,适合TPM开发</span>    <span style="color: #0000ff;">if</span>(isset ($_SERVER[<span style="color: #800000;">'</span><span style="color: #800000;">HTTP_CLIENT</span><span style="color: #800000;">'</span>]) &&<span style="color: #800000;">'</span><span style="color: #800000;">PhoneClient</span><span style="color: #800000;">'</span>==$_SERVER[<span style="color: #800000;">'</span><span style="color: #800000;">HTTP_CLIENT</span><span style="color: #800000;">'</span><span style="color: #000000;">])        </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;    </span><span style="color: #008000;">//</span><span style="color: #008000;">如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息</span>    <span style="color: #0000ff;">if</span> (isset ($_SERVER[<span style="color: #800000;">'</span><span style="color: #800000;">HTTP_VIA</span><span style="color: #800000;">'</span><span style="color: #000000;">]))        </span><span style="color: #008000;">//</span><span style="color: #008000;">找不到为flase,否则为true</span>        <span style="color: #0000ff;">return</span> stristr($_SERVER[<span style="color: #800000;">'</span><span style="color: #800000;">HTTP_VIA</span><span style="color: #800000;">'</span>], <span style="color: #800000;">'</span><span style="color: #800000;">wap</span><span style="color: #800000;">'</span>) ? <span style="color: #0000ff;">true</span> : <span style="color: #0000ff;">false</span><span style="color: #000000;">;    </span><span style="color: #008000;">//</span><span style="color: #008000;">判断手机发送的客户端标志,兼容性有待提高</span>    <span style="color: #0000ff;">if</span> (isset ($_SERVER[<span style="color: #800000;">'</span><span style="color: #800000;">HTTP_USER_AGENT</span><span style="color: #800000;">'</span><span style="color: #000000;">])) {        $clientkeywords </span>=<span style="color: #000000;"> array(            </span><span style="color: #800000;">'</span><span style="color: #800000;">nokia</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">sony</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">ericsson</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">mot</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">samsung</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">htc</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">sgh</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">lg</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">sharp</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">sie-</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">philips</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">panasonic</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">alcatel</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">lenovo</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">iphone</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">ipod</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">blackberry</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">meizu</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">android</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">netfront</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">symbian</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">ucweb</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">windowsce</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">palm</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">operamini</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">operamobi</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">openwave</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">nexusone</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">cldc</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">midp</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">wap</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">mobile</span><span style="color: #800000;">'</span><span style="color: #000000;">        );        </span><span style="color: #008000;">//</span><span style="color: #008000;">从HTTP_USER_AGENT中查找手机浏览器的关键字</span>        <span style="color: #0000ff;">if</span> (preg_match(<span style="color: #800000;">"</span><span style="color: #800000;">/(</span><span style="color: #800000;">"</span> . implode(<span style="color: #800000;">'</span><span style="color: #800000;">|</span><span style="color: #800000;">'</span>, $clientkeywords) . <span style="color: #800000;">"</span><span style="color: #800000;">)/i</span><span style="color: #800000;">"</span>, strtolower($_SERVER[<span style="color: #800000;">'</span><span style="color: #800000;">HTTP_USER_AGENT</span><span style="color: #800000;">'</span><span style="color: #000000;">]))) {            </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;        }    }    </span><span style="color: #008000;">//</span><span style="color: #008000;">协议法,因为有可能不准确,放到最后判断</span>    <span style="color: #0000ff;">if</span> (isset ($_SERVER[<span style="color: #800000;">'</span><span style="color: #800000;">HTTP_ACCEPT</span><span style="color: #800000;">'</span><span style="color: #000000;">])) {        </span><span style="color: #008000;">//</span><span style="color: #008000;"> 如果只支持wml并且不支持html那一定是移动设备        </span><span style="color: #008000;">//</span><span style="color: #008000;"> 如果支持wml和html但是wml在html之前则是移动设备</span>        <span style="color: #0000ff;">if</span> ((strpos($_SERVER[<span style="color: #800000;">'</span><span style="color: #800000;">HTTP_ACCEPT</span><span style="color: #800000;">'</span>], <span style="color: #800000;">'</span><span style="color: #800000;">vnd.wap.wml</span><span style="color: #800000;">'</span>) !== <span style="color: #0000ff;">false</span>) && (strpos($_SERVER[<span style="color: #800000;">'</span><span style="color: #800000;">HTTP_ACCEPT</span><span style="color: #800000;">'</span>], <span style="color: #800000;">'</span><span style="color: #800000;">text/html</span><span style="color: #800000;">'</span>) === <span style="color: #0000ff;">false</span> || (strpos($_SERVER[<span style="color: #800000;">'</span><span style="color: #800000;">HTTP_ACCEPT</span><span style="color: #800000;">'</span>], <span style="color: #800000;">'</span><span style="color: #800000;">vnd.wap.wml</span><span style="color: #800000;">'</span>) '<span style="color: #800000;">HTTP_ACCEPT</span><span style="color: #800000;">'</span>], <span style="color: #800000;">'</span><span style="color: #800000;">text/html</span><span style="color: #800000;">'</span><span style="color: #000000;">)))) {            </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;        }    }    </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;} 配置</span>/Application/Home/Controller/CommonController.<span style="color: #0000ff;">class</span><span style="color: #000000;">.php [公共类]【详见代码段2】</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> function _initialize(){    </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(isMobile()){        C(</span><span style="color: #800000;">'</span><span style="color: #800000;">DEFAULT_MODULE</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">Mobile</span><span style="color: #800000;">'</span><span style="color: #000000;">);    }}配置</span>/Application/Mobile/Controller/CommonController.<span style="color: #0000ff;">class</span><span style="color: #000000;">.php [公共类]【详见代码段3】</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> function _initialize(){    </span><span style="color: #0000ff;">if</span>(!<span style="color: #000000;">isMobile()){        C(</span><span style="color: #800000;">'</span><span style="color: #800000;">DEFAULT_MODULE</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">Home</span><span style="color: #800000;">'</span><span style="color: #000000;">);    }}</span>
로그인 후 복사

 

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿