Yii URL 管理 HTTPS

2023-12-01

我使用代码来分隔网站中的 HTTPS 和 HTTP 页面

问题是:当我使用 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/


最好使用过滤器在控制器级别进行管理。

在你的组件目录中设置 2 个过滤器HttpsFilter and HttpFilter如下:-

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;
    }

}

and

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
       );

See http://www.yiiframework.com/doc/guide/1.1/en/basics.controller#filter(以及对此的评论)。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Yii URL 管理 HTTPS 的相关文章

  • 无法打开输入文件应用程序/控制台

    我安装了 wamp 服务器和 Symfony2 框架的副本 我正在尝试使用以下命令创建一个捆绑包 php app console generate bundle nampespace IDP IDP Bundle format yml 我的
  • php将多维数组内爆为制表符分隔行

    我有一个多维数组 BlockData 其中有 13 个维度和 n 个数组元素 我需要将此数组内爆回单个长字符串 其中元素由 n 换行和尺寸由 t tabs 我尝试过使用array map 功能没有成功 需要帮助来完成此任务 请帮忙 这可以使
  • PHP 裁剪图像以固定宽度和高度而不丢失尺寸比例

    我希望创建尺寸为 100 像素 x 100 像素的缩略图 我看过很多解释这些方法的文章 但如果要保持尺寸比 大多数文章最终都会有宽度 高度 例如 我有一个 450 像素 x 350 像素的图像 我想裁剪为 100px x 100px 如果我
  • 根据选择值显示/隐藏字段

    我试图根据我选择的字段之一的值显示和隐藏一些表单字段 我希望使用数组来保存每个选择值应该显示的内容和不应该显示的内容 以将我从大量的 switch 语句中拯救出来 但无法弄清楚如何做到这一点 我正在使用 PHP 和 jQuery 任何帮助都
  • 正则表达式将从文本文件中提取句子

    我需要一个正则表达式来从文本文件中提取句子 示例文本 以 2004 年底发生的亚洲海啸灾难为例 对 Google 新闻 http news google com 的查询在一个月内 1 月 17 日 返回了超过 80 000 篇有关该事件的在
  • 将变量设置为函数调用以在 PHP 中的 if 语句中使用

    好的 我正在做一些 Wordpress 编辑 并且编写了一个 if 语句 正如您所看到的 这使用函数调用作为变量 这是因为函数调用会调用当前页面的名称 这很好 然而 当我这样做时 它也往往会与页面上的标题相呼应 这是有道理的 我可能正在尝试
  • PHP 和 MySQL - 高效处理多个一对多关系

    我正在寻求一些有关使用 MySQL 和 PHP 检索和显示数据的最佳方法的建议 我有 3 个表 所有一对多关系如下 Each SCHEDULE有很多覆盖每个覆盖都有很多地点 我想检索这些数据 以便它可以全部显示在单个 PHP 页面上 例如列
  • Stripe 支付网关使用 PayumBundle 创建定期付款

    我在用支付包 https github com Payum PayumBundle将 Stripe 支付网关集成到我的 symfony2 应用程序中 我可以创建成功的直接付款 但无法创建定期付款 因为捆绑包的文档非常差 我的问题是如何使用
  • 如何使用 PHP 从图像文件中读取 Lightroom 关键字?

    我有一个照片社区 www jungledragon com http www jungledragon com 允许用户上传照片 我的平台是 PHP CodeIgniter 作为上传过程的一部分 我已经使用 PHP 读取 EXIF 信息ex
  • yii2 无线电内联 Html 帮助器

    我在 yii2 中有这个 radioList Html radioList abc null new class gt form control input sm 它生成这个 div class radio 但我想要 div class r
  • PHP 的同义词库类或 API [编辑]

    TL DR 摘要 我需要一个命令行应用程序 我可以用它来获取同义词和其他相关单词 它需要是多语言的并且跨平台工作 任何人都可以为我推荐一个合适的程序 或者帮助我使用我已经找到的程序吗 谢谢 更长的版本 我的任务是用 PHP 编写一个系统 该
  • PHP—array_merge_recursive() - 相同键没有数组

    php a php gt data1 tag gt div classes gt 1 2 3 php gt data2 tag gt section classes gt 2 3 4 5 6 php gt result array merg
  • 多维数组内的移动

    我有一个用表格显示的数组 如何使用用户输入进行移动 目前 0 被分配给每个数组 但我计划为该数组分配其他值 我的问题是 如何使用用户输入在数组内向上 向下 向右 向左移动和对角移动 Array 0 gt Array 0 gt 0 1 gt
  • Laravel 验证规则仅针对字母

    我正在尝试添加验证规则以仅接受信件 我正在使用regex规则 但它仍然不起作用 下面是我的代码 Validate request input this gt validate request name gt required regex p
  • 支持通过 OAuth 进行 Facebook/Twitter 身份验证的 CAS 服务器

    我正在寻找一个支持 Facebook Twitter 通过 OAuth 进行单点登录身份验证的 CAS 服务器 我检查过 JASIG CAS 服务器 但它看起来不支持它们 我的 java web 应用程序基于 Spring Security
  • 如何在 PHP 中使用 file_get_contents 获取图像的 MIME 类型

    我需要获取图像的 MIME 类型 但我只有图像的正文file get contents 是否有可能获取 MIME 类型 是的 你可以这样得到它 file info new finfo FILEINFO MIME TYPE mime type
  • 如何使用 jQuery 通过 Ajax 发送复选框数组的值?

    我有一个包含很多表单字段的表单 12 x n 行 每行中的第一个字段 代表产品 是一个类似于以下内容的复选框
  • NetBeans 代码模板 ${date}?

    我在 Eclipse 中有这个代码模板 自 日期 起 当输入时我得到这样的东西 自2009年8月4日起 但是当我添加相同的模板时 自 日期 起 到 NetBeans 它输出 自日期以来 有人可以帮忙吗 还没有答案吗 这在 Netbeans
  • 在生产服务器上使用 Subversion 使文件生效的最佳方法是什么?

    目前我已经设置了 subversion 这样当我在 Eclipse PDT 中进行更改时 我可以提交更改 它们将保存在 home administrator 中项目文件 该文件具有 subversion 推荐的 branches tags
  • 获取url,给定的url重定向到

    我从 rss 链接中挖掘数据并获得一堆网址 例如 http feedproxy google com r electricpig 3 qoF8XbocUbE http feedproxy google com r electricpig 3

随机推荐