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

WBOY
Freigeben: 2016-06-13 12:23:56
Original
1034 Leute haben es durchsucht

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>
Nach dem Login kopieren

 

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage