php 如何在codeigniter中强制使用ssl?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42625147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:39:43  来源:igfitidea点击:

How to force ssl in codeigniter?

phpcodeigniterssl

提问by fustaki

I am using codeigniter 3. How do I force SSL connection to my web pages so that all the pages are loaded with the green padlock icon beside it?

我正在使用 codeigniter 3. 如何强制 SSL 连接到我的网页,以便所有页面都加载了旁边的绿色挂锁图标?

Note: How can I do this without having to edit htaccess file ?

注意:如何在不必编辑 htaccess 文件的情况下执行此操作?

回答by Rabin Lama Dong

Open config file from location application/config/config.phpand enable or set hooks to true like this:

从位置打开配置文件application/config/config.php并启用或设置钩子为 true,如下所示:

$config['enable_hooks'] = TRUE;

Then create a new file named hooks.phpinside the configfolder (i.e. application/config/hooks.php) and add the following code in it:

然后hooks.phpconfig文件夹内新建一个名为application/config/hooks.php的文件,并在其中添加以下代码:

$hook['post_controller_constructor'][] = array(
    'function' => 'redirect_ssl',
    'filename' => 'ssl.php',
    'filepath' => 'hooks'
);

Now create a new directory named hooksinside the applicationfolder (i.e. application/hooks) and then create a new file named ssl.phpinside the hooksfolder (i.e. application/hooks/ssl.php).

现在hooksapplication文件夹内创建一个名为application/hooks的新目录,然后ssl.phphooks文件夹内创建一个名为application/hooks/ssl.php的新文件。

Add the following code in the ssl.phpfile:

ssl.php文件中添加以下代码:

function redirect_ssl() {
    $CI =& get_instance();
    $class = $CI->router->fetch_class();
    $exclude =  array('client');  // add more controller name to exclude ssl.
    if(!in_array($class,$exclude)) {
        // redirecting to ssl.
        $CI->config->config['base_url'] = str_replace('http://', 'https://', $CI->config->config['base_url']);
        if ($_SERVER['SERVER_PORT'] != 443) redirect($CI->uri->uri_string());
    } else {
        // redirecting with no ssl.
        $CI->config->config['base_url'] = str_replace('https://', 'http://', $CI->config->config['base_url']);
        if ($_SERVER['SERVER_PORT'] == 443) redirect($CI->uri->uri_string());
    }
}

回答by fustaki

  1. Change base_urlin your config:

    $config['base_url'] = 'https://www.my-site.com/';
    
  2. Provide a certificate for your secure connection
  3. Redirect incoming traffic from httpto https:

    RewriteCond %{HTTPS} off
    RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
  1. 更改base_url您的配置:

    $config['base_url'] = 'https://www.my-site.com/';
    
  2. 为您的安全连接提供证书
  3. 将传入流量从 重定向httphttps

    RewriteCond %{HTTPS} off
    RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    

回答by Jose Alexis Correa Valencia

Codeigniter(GAE): best practice to redirect http to https in the ssl.php file:

Codeigniter(GAE):在 ssl.php 文件中将 http 重定向到 https 的最佳实践:

function force_ssl() {
    $server=$_SERVER["SERVER_NAME"];
    $uri=$_SERVER["REQUEST_URI"];
    if ($_SERVER['HTTPS'] == 'off') {
        redirect("https://{$server}{$uri}");
    }
}

回答by Anthony

The first thing you need to check is for reference links from insecure elements on page source such as images loaded with Http.enter image description here

您需要检查的第一件事是来自页面源上不安全元素的参考链接,例如使用 Http 加载的图像。在此处输入图片说明

enter image description here

在此处输入图片说明