CoreThink開發(十一)首頁控制器判斷行動裝置還是PC並做相應處理

來源:互聯網
上載者:User

標籤:lca   關鍵字   set   切換   windows   判斷   splay   htc   https   

在home模組Index控制器添加判斷代碼
application\Home\Controller\IndexController.class.php

<?php// +----------------------------------------------------------------------// | OpenCMF [ Simple Efficient Excellent ]// +----------------------------------------------------------------------// | Copyright (c) 2014 http://www.opencmf.cn All rights reserved.// +----------------------------------------------------------------------// | Author: jry <[email protected]>// +----------------------------------------------------------------------namespace Home\Controller;use Think\Controller;/** * 前台預設控制器 * @author jry <[email protected]> */class IndexController extends HomeController {    /**     * 預設方法     * @author jry <[email protected]>     */    public function index() {        Cookie(‘__forward__‘, C(‘HOME_PAGE‘));        if ($this->isMobile()) {                 $this->redirect(‘/cms/index/lists/cid/1.html‘);//使手機重新導向到Mobile模組        }        if (C(‘INDEX_URL‘)) {            if (stristr(C(‘INDEX_URL‘), ‘http://‘) || stristr(C(‘INDEX_URL‘), ‘https://‘)) {                redirect(C(‘INDEX_URL‘));            } else {                $this->redirect(C(‘INDEX_URL‘));            }        }        $this->assign(‘meta_title‘, "首頁");        $this->display();    }     function isMobile(){        // 如果有HTTP_X_WAP_PROFILE則一定是行動裝置        if (isset ($_SERVER[‘HTTP_X_WAP_PROFILE‘]))            return true;        //此條摘自TPM智能切換模板引擎,適合TPM開發        if(isset ($_SERVER[‘HTTP_CLIENT‘]) &&‘PhoneClient‘==$_SERVER[‘HTTP_CLIENT‘])            return true;        //如果via資訊含有wap則一定是行動裝置,部分服務商會屏蔽該資訊        if (isset ($_SERVER[‘HTTP_VIA‘]))            //找不到為flase,否則為true            return stristr($_SERVER[‘HTTP_VIA‘], ‘wap‘) ? true : false;        //判斷手機發送的用戶端標誌,相容性有待提高        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‘]))) {                return true;            }        }        //協議法,因為有可能不準確,放到最後判斷        if (isset ($_SERVER[‘HTTP_ACCEPT‘])) {            // 如果只支援wml並且不支援html那一定是行動裝置            // 如果支援wml和html但是wml在html之前則是行動裝置            if ((strpos($_SERVER[‘HTTP_ACCEPT‘], ‘vnd.wap.wml‘) !== false) && (strpos($_SERVER[‘HTTP_ACCEPT‘], ‘text/html‘) === false || (strpos($_SERVER[‘HTTP_ACCEPT‘], ‘vnd.wap.wml‘) < strpos($_SERVER[‘HTTP_ACCEPT‘], ‘text/html‘)))) {                return true;            }        }        return false;        }}

  isMobile判斷方法來自網路.

CoreThink開發(十一)首頁控制器判斷行動裝置還是PC並做相應處理

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.