【问题标题】:Forcing SSL in codeigniter with a helper使用助手在 codeigniter 中强制 SSL
【发布时间】:2012-07-13 18:28:20
【问题描述】:

我正在尝试对我的购物车应用程序采用 ssl 路由解决方案,以便某些页面被“强制”为 https:// 页面和 visa-verse。通过研究,我遇到了许多解决方案,这些解决方案涉及在帮助程序、挂钩或控制器中使用代码。我已经尝试了其中的一些解决方案,在每个解决方案中,切换到 https:// 页面时都会出现重定向错误。

这是我尝试过的最后一个版本(在这里找到:http://nigel.mcbryde.com.au/2009/03/working-with-ssl-in-codeigniter):

在 application/helper 中创建一个名为 ssl_helper.php 的文件

if (!function_exists('force_ssl'))
{
    function force_ssl()
    {
        $CI =& get_instance();
        $CI->config->config['base_url'] =
                 str_replace('http://', 'https://',
                 $CI->config->config['base_url']);
        if ($_SERVER['SERVER_PORT'] != 443)
        {
            redirect($CI->uri->uri_string());
        }
    }
}

function remove_ssl()
{
    $CI =& get_instance();
    $CI->config->config['base_url'] =
                  str_replace('https://', 'http://',
                  $CI->config->config['base_url']);
    if ($_SERVER['SERVER_PORT'] != 80)
    {
        redirect($CI->uri->uri_string());
    }
}

加载帮助程序,然后在任何需要 ssl 的控制器的构造函数中,只需插入:

force_ssl();

在您不想放置 ssl 的每个控制器中:

if (function_exists('force_ssl')) remove_ssl();

如上所述,当使用它时,我陷入重定向循环并收到错误(尽管 url 应该有 https://)。我的问题可能出在我的 .htaccess 文件中吗?这是我的 .htaccess 文件:

<IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteBase /store
   RewriteCond $1 !^(index.php\.php|resources|robots\.txt)
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
</IfModule>

我已经玩了一个多星期了!有什么建议吗?

【问题讨论】:

    标签: .htaccess codeigniter ssl


    【解决方案1】:

    我回答了类似的问题here: https://stackoverflow.com/questions/1500527/how-to-use-ssl-with-codeigniter/1500558#1500558

    但是,简而言之:

    <IfModule mod_rewrite.c>
       RewriteEngine On
       RedirectPermanent /sslfolder https://www.yoursite.com/sslfolder
    </IfModule>
    

    【讨论】:

    • 谢谢,我在研究过程中看到了这个解决方案。如果我不能像其他人那样工作,我可以使用这个解决方案来制作整个购物车应用程序 https。我更喜欢有一个文件夹。
    • 使用您的解决方案使整个购物车 https
    • 它会导致更多的 CPU 负载,但在我看来,这始终是正确的方法。
    【解决方案2】:

    大家好,通过在配置中设置 base_url 来检查答案,它将自动在 http 和 https 之间切换

    force ssl without helper

    【讨论】:

      【解决方案3】:

      如果你使用 force_ssl 函数,这里是重定向循环的解决方案。

      function check_ssl()
      {
          $CI =& get_instance();
          $class = $CI->router->fetch_class();       
          $ssl = array('Register', 'SignIn', 'Recover');        
          if(in_array($class, $ssl))
          {
              force_ssl();
          }
          else
          {
              unforce_ssl();
          }
      }
      function force_ssl()
      {        
          $CI =& get_instance();
          $CI->config->set_item('base_url', str_replace('http://', 'https://', config_item('base_url'))); 
          if ($_SERVER['SERVER_PORT'] != 443) // it will loop if you change it to !==
          {
              redirect($CI->uri->uri_string());
          }
      }
      function unforce_ssl()
      {
          $CI =& get_instance();
          $CI->config->set_item('base_url', str_replace('https://', 'http://', config_item('base_url')));
          if ($_SERVER['SERVER_PORT'] != 80) // it will loop if you change it to !==
          {
              redirect($CI->uri->uri_string());
          }   
      }
      

      【讨论】:

        猜你喜欢
        • 2012-07-14
        • 2019-04-12
        • 1970-01-01
        • 1970-01-01
        • 2013-03-13
        • 2023-03-02
        • 2014-12-04
        • 2012-09-09
        • 1970-01-01
        相关资源
        最近更新 更多