Yii的URL管理HTTPS(Yii URL Management HTTPS)

2019-07-01 18:44发布

即时通讯使用代码分离是HTTPS和HTTP网页在我的网站

问题是:当IM在HTTP,链接到HTTPS不具备WWW,反之亦然。 我没有找到脚本的问题。

 public function createUrl($route, $params = array(), $ampersand = '&') { $url = parent::createUrl($route, $params, $ampersand); // If already an absolute URL, return it directly if (strpos($url, 'http') === 0) { return $url; } // Check if the current protocol matches the expected protocol of the route // If not, prefix the generated URL with the correct host info. $secureRoute = $this->isSecureRoute($route); if (Yii::app()->request->isSecureConnection) { return $secureRoute ? $url : 'http://' . Yii::app()->request->serverName . $url; } else { return $secureRoute ? 'https://' . Yii::app()->request->serverName . $url : $url; } } public function parseUrl($request) { $route = parent::parseUrl($request); // Perform a 301 redirection if the current protocol // does not match the expected protocol $secureRoute = $this->isSecureRoute($route); $sslRequest = $request->isSecureConnection; if ($secureRoute !== $sslRequest) { $hostInfo = $secureRoute ? 'https://' . Yii::app()->request->serverName : 'http://' . Yii::app()->request->serverName; if ((strpos($hostInfo, 'https') === 0) xor $sslRequest) { $request->redirect($hostInfo . $request->url, true, 301); } } return $route; } private $_secureMap; /** * @param string the URL route to be checked * @return boolean if the give route should be serviced in SSL mode */ protected function isSecureRoute($route) { if ($this->_secureMap === null) { foreach ($this->secureRoutes as $r) { $this->_secureMap[strtolower($r)] = true; } } $route = strtolower($route); if (isset($this->_secureMap[$route])) { return true; } else { return ($pos = strpos($route, '/')) !== false && isset($this->_secureMap[substr($route, 0, $pos)]); } } 

}

代码改编自: http://www.yiiframework.com/wiki/407/url-management-for-websites-with-secure-and-nonsecure-pages/

Answer 1:

这是更好地使用过滤器控制器级别来管理这一点。

在你的组件目录设置2个过滤器HttpsFilterHttpFilter如下: -

class HttpsFilter extends CFilter {

    protected function preFilter( $filterChain ) {
        if ( !Yii::app()->getRequest()->isSecureConnection ) {
            # Redirect to the secure version of the page.
            $url = 'https://' .
                Yii::app()->getRequest()->serverName .
                Yii::app()->getRequest()->requestUri;
                Yii::app()->request->redirect($url);
            return false;
        }
        return true;
    }

}

class HttpFilter extends CFilter {

    protected function preFilter( $filterChain ) {
        if ( Yii::app()->getRequest()->isSecureConnection ) {
            # Redirect to the secure version of the page.
                $url = 'http://' .
                Yii::app()->getRequest()->serverName .
                Yii::app()->getRequest()->requestUri;
                Yii::app()->request->redirect($url);
            return false;
        }
        return true;
    } 
}

然后在使用该过滤器,任选地通过动作的每个控制器力的https:

class SiteController extends Controller {

    public function filters()
    {
        return array(
            'https +index', // Force https, but only on login page
        );
    }
}

编辑:如果filters()上面似乎功能不能为你工作,而不是尝试

return array(
           array('HttpsFilter +index'), // Force https, but only on login page
       );

见http://www.yiiframework.com/doc/guide/1.1/en/basics.controller#filter (和评论就可以了)。



文章来源: Yii URL Management HTTPS
标签: php yii