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
How to force ssl in codeigniter?
提问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.php
and enable or set hooks to true like this:
从位置打开配置文件application/config/config.php
并启用或设置钩子为 true,如下所示:
$config['enable_hooks'] = TRUE;
Then create a new file named hooks.php
inside the config
folder (i.e. application/config/hooks.php) and add the following code in it:
然后hooks.php
在config
文件夹内新建一个名为application/config/hooks.php的文件,并在其中添加以下代码:
$hook['post_controller_constructor'][] = array(
'function' => 'redirect_ssl',
'filename' => 'ssl.php',
'filepath' => 'hooks'
);
Now create a new directory named hooks
inside the application
folder (i.e. application/hooks) and then create a new file named ssl.php
inside the hooks
folder (i.e. application/hooks/ssl.php).
现在hooks
在application
文件夹内创建一个名为application/hooks的新目录,然后ssl.php
在hooks
文件夹内创建一个名为application/hooks/ssl.php的新文件。
Add the following code in the ssl.php
file:
在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
Change
base_url
in your config:$config['base_url'] = 'https://www.my-site.com/';
- Provide a certificate for your secure connection
Redirect incoming traffic from
http
tohttps
:RewriteCond %{HTTPS} off RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
更改
base_url
您的配置:$config['base_url'] = 'https://www.my-site.com/';
- 为您的安全连接提供证书
将传入流量从 重定向
http
到https
: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}");
}
}