如何让 CodeIgniter 使用 SSL 加载特定页面?

发布于 2024-08-06 11:20:43 字数 254 浏览 5 评论 0原文

如何让 CodeIgniter 使用 SSL 加载特定页面?我有一个 apache2/mode_ssl 服务器。 mod_ssl 使用与非安全页面不同的文档根。例如,https(端口 443)将提供 /var/www/ssl_html/ 之外的页面,而 http(端口 80)将提供 /var/www/html/ 之外的页面>。我如何让 CodeIgniter 能够很好地使用此设置?

How can I have CodeIgniter load specific pages using SSL? I have an apache2/mode_ssl server. mod_ssl uses a different document root than non-secure pages. For example, https (port 443) would serve pages out of /var/www/ssl_html/ And http (port 80) serves pages out of /var/www/html/. How would I get CodeIgniter to play nice with this setup?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

绝情姑娘 2024-08-13 11:20:43

解决这个问题的方法很少。

选项 1:

我可能会将代码部署到这两个文件夹,然后在文件:/system/application/config/config.php 中,将页面设置为:

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

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

然后在非 ssl VirtualHost 中文件夹,设置您的配置以按文件夹将受保护的页面重定向到 SSL 站点:

RedirectPermanent /sslfolder https://www.yoursite.com/sslfolder

选项 2:

将所有内容发送到 SSL 并将所有代码保存在一个文件夹

/system/application/config/config.php 中,设置您的页面到:

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

其他选项

还有一些更古怪的方法可以通过 header() 重定向等来实现此目的,但我认为您不想为此选项维护不同的代码库。我不推荐这样做,但你可以这样做:

$config['base_url'] = “http://” . $_SERVER['http_host'] . “/”;

There are few ways to tackle this.

Option 1:

I would probably have the code deployed to both folders, then in the file: /system/application/config/config.php, set your page to:

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

or

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

Then in your non-ssl VirtualHost folder, set your config to redirect protected pages by folder to the SSL site:

RedirectPermanent /sslfolder https://www.yoursite.com/sslfolder

Option 2:

Send everything to SSL and keep all your code in one folder

/system/application/config/config.php, set your page to:

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

Other Options

There are some more hacky ways to do this with header() redirects, etc. but I don't think you want to maintain different code bases for this option. I don't recommend this but you could do something like:

$config['base_url'] = “http://” . $_SERVER['http_host'] . “/”;
堇年纸鸢 2024-08-13 11:20:43

在 application/config/config.php 中,将 base_url 设置为:

$config['base_url'] = ($_SERVER['SERVER_PORT'] == 443 ? 'https' : 'http') 。 "://{$_SERVER['HTTP_HOST']}/";

这将允许它在任何域上工作,如果您在本地测试,这会很方便。

In application/config/config.php, set base_url to:

$config['base_url'] = ($_SERVER['SERVER_PORT'] == 443 ? 'https' : 'http') . "://{$_SERVER['HTTP_HOST']}/";

This will allow it to work on any domain, which is convenient if you test locally.

墟烟 2024-08-13 11:20:43

我将以下几行放入 ./application/config/config.php 文件中,它运行良好:

$protocol = ( isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ) ? 'https' : 'http';
$config['base_url'] = $protocol.'://www.yoursite.com';

I put following lines in ./application/config/config.php file and it works perfectly:

$protocol = ( isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ) ? 'https' : 'http';
$config['base_url'] = $protocol.'://www.yoursite.com';
空城仅有旧梦在 2024-08-13 11:20:43

我将维护两组代码,并使用 post_controller_constructor 挂钩处理强制重定向到 HTTPS 页面。该钩子将在呈现每个页面之前调用,以检查访问者是否应根据访问者在您网站上的当前位置将其重定向到 HTTPS。

步骤 1

创建文件:system/application/hooks/SecureAccount.php

<?php

class SecureAccount
{
    var $obj;

    //--------------------------------------------------
    //SecureAccount constructor
    function SecureAccount()
    {
        $this->obj =& get_instance();
    }

    //--------------------------------------------------
    //Redirect to https if in the account area without it
    function index()
    {
        if(empty($_SERVER["HTTPS"]) || $_SERVER["HTTPS"] !== 'on')
        {
            $this->obj =& get_instance();
            $this->obj->load->helper(array('url', 'http'));

            $current = current_url();
            $current = parse_url($current);

            if((stripos($current['path'], "/account/") !== false))
            {
                $current['scheme'] = 'https';

                redirect(http_build_url('', $current), 'refresh');
            }
        }
    }
}
?>

步骤 2

在函数中自定义应强制站点区域使用 HTTPS 的路径。

步骤 3

将以下内容添加到 system/application/config/hooks.php

/* Force HTTPS for account area */
$hook['post_controller_constructor'] = array(
                                'class'    => 'SecureAccount',
                                'function' => 'index',
                                'filename' => 'SecureAccount.php',
                                'filepath' => 'hooks',
                                'params'   => array()
                                );

I would maintain two sets of the code and handle a forced redirect to HTTPS pages with a post_controller_constructor hook. The hook will be called before each page is rendered to check if the visitor should be redirected to HTTPS based on their current location on your site.

STEP 1

Create file: system/application/hooks/SecureAccount.php

<?php

class SecureAccount
{
    var $obj;

    //--------------------------------------------------
    //SecureAccount constructor
    function SecureAccount()
    {
        $this->obj =& get_instance();
    }

    //--------------------------------------------------
    //Redirect to https if in the account area without it
    function index()
    {
        if(empty($_SERVER["HTTPS"]) || $_SERVER["HTTPS"] !== 'on')
        {
            $this->obj =& get_instance();
            $this->obj->load->helper(array('url', 'http'));

            $current = current_url();
            $current = parse_url($current);

            if((stripos($current['path'], "/account/") !== false))
            {
                $current['scheme'] = 'https';

                redirect(http_build_url('', $current), 'refresh');
            }
        }
    }
}
?>

STEP 2

Customize the path in the function for which areas of your site should be forced to use HTTPS.

STEP 3

Add the following to system/application/config/hooks.php

/* Force HTTPS for account area */
$hook['post_controller_constructor'] = array(
                                'class'    => 'SecureAccount',
                                'function' => 'index',
                                'filename' => 'SecureAccount.php',
                                'filepath' => 'hooks',
                                'params'   => array()
                                );
过度放纵 2024-08-13 11:20:43

我可以很容易地,我只定义:

$config['base_url'] = '';

CI 自动获取base_url =D

I can easily, I only define:

$config['base_url'] = '';

CI get base_url automaticaly =D

箹锭⒈辈孓 2024-08-13 11:20:43

我解决了在 config.php 目录麻烦的问题

$config['ssl_active'] = false;
if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) {
    $config['ssl_active'] = true;
}

$config['base_url'] = ($config['ssl_active'] === true )?"https":"http" . $_SERVER['HTTP_HOST'];

,可以使用简单的符号链接 ln -s

I solved the problem with in config.php

$config['ssl_active'] = false;
if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) {
    $config['ssl_active'] = true;
}

$config['base_url'] = ($config['ssl_active'] === true )?"https":"http" . $_SERVER['HTTP_HOST'];

The directory trouble, you can use a simple simbolic link ln -s

巡山小妖精 2024-08-13 11:20:43

这对我有用。
我们的服务器都有 $_SERVER['SERVER_PORT']$_SERVER['HTTP_X_FORWARDED_PORT']

我们应该首先检查是否 $_SERVER['HTTP_X_FORWARDED_PORT'] 可用并使用它代替 $_SERVER['SERVER_PORT']

$config['base_url'] =
( ( ( empty($_SERVER['HTTP_X_FORWARDED_PORT']) ? $_SERVER['SERVER_PORT'] : $_SERVER['HTTP_X_FORWARDED_PORT'] ) == 443 ? 'https' : 'http') . "://" . $_SERVER['HTTP_HOST'] )
. str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']) ;

在我们的 Linux 服务器上测试了这个...


顺便说一句,它是基于 Skyler 之前的答案。

$config['base_url'] = ($_SERVER['SERVER_PORT'] == 443 ? 'https' : 'http') . "://{$_SERVER['HTTP_HOST']}/";

This is what worked for me.
Our server both have $_SERVER['SERVER_PORT'] and $_SERVER['HTTP_X_FORWARDED_PORT']

We should check first if $_SERVER['HTTP_X_FORWARDED_PORT'] is available and use this instead of $_SERVER['SERVER_PORT']

$config['base_url'] =
( ( ( empty($_SERVER['HTTP_X_FORWARDED_PORT']) ? $_SERVER['SERVER_PORT'] : $_SERVER['HTTP_X_FORWARDED_PORT'] ) == 443 ? 'https' : 'http') . "://" . $_SERVER['HTTP_HOST'] )
. str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']) ;

tested this on our linux server...


btw, it's based on skyler's answer before which was.

$config['base_url'] = ($_SERVER['SERVER_PORT'] == 443 ? 'https' : 'http') . "://{$_SERVER['HTTP_HOST']}/";
赠意 2024-08-13 11:20:43

我想到的解决方案是使用 str_replace() ,这样

$base_url_str = base_url(); 
$secure_base_url = str_replace("http","https",$base_url_str );

每当我需要一个安全位置时,我就使用 $secure_base_url 而不是 base_url(),
假设您的 base_url() 是 http://www.example.com 那么 $secure_base_url 将是 https://www.example.com

the solution that came to my mind is to use str_replace() like so

$base_url_str = base_url(); 
$secure_base_url = str_replace("http","https",$base_url_str );

whenever I need a secure location , i use $secure_base_url instead of base_url(),
so let's say your base_url() is http://www.example.com then $secure_base_url will be https://www.example.com

梦境 2024-08-13 11:20:43

另一个问题与此类似,

您可以使用协议相对 url 这将保持 CI 中链接的持久性。

在您的 config.php 中,而不是:

$config['base_url'] = 'http://example.com/';

put;

$config['base_url'] = '//example.com/';

有关协议相对链接的信息此处

another question is similar to this

you can use a protocol-relative url which will keep persistence in the linking in CI.

in your config.php instead of:

$config['base_url'] = 'http://example.com/';

put;

$config['base_url'] = '//example.com/';

information about protocol-relative links here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文