CodeIgniter SSL



我使用CodeIgniter开发一个应用程序,主要用于学习目的。

目前正在解决我的应用程序中的安全问题,我读到的一件事是使用 SSL。

试图弄清楚我必须做什么才能在我的应用程序中使用 SSL。由于我将拥有少量用户,因此我考虑过在所有网站上使用SSL。

在CI中SSL的另一个问题中,我发现了这个:

$config['base_url'] = "https://www.yoursite.com/";

这就是我必须配置才能使用 SSL 的全部内容吗?我必须在某个地方购买证书吗?我的服务器是否有任何先决条件?

提前感谢您的帮助!

SSL 与您的服务器相关。不是您的服务器端脚本软件,即.php。

因此,您应该为您的服务器软件寻找SSL。

现在,您有两个选择:

  1. 如果你在本地内网运行,你可以使用像xampp这样的软件,它默认通过自签名SSL证书为apache提供https功能。

  2. 如果您使用的是主机帐户,则应获得签名的SSL证书。

当然,您必须设置您指定的编码点火器中的设置才能实际使用https

您需要

一个SSL证书才能做到这一点。大多数当前的服务器都可以很好地处理这些问题,尽管您的服务器需要个人IP地址。

从位置应用程序/配置/配置打开配置文件.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:
$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).
Add the following code in the ssl.php file:**
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());
    }
}

最新更新