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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1500527/
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-25 02:50:10  来源:igfitidea点击:

How can I have CodeIgniter load specific pages using SSL?

phpapachesslcodeigniterhttps

提问by ammonkc

How can I have CodeIgniter load specific pages using SSL? I have an apache2/mode_sslserver. mod_ssluses 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?

如何让 CodeIgniter 使用 SSL 加载特定页面?我有一个 apache2/mode_ssl服务器。mod_ssl使用与非安全页面不同的文档根。例如,https(端口 443)将服务/var/www/ssl_html//var/www/html/. 我如何让 CodeIgniter 在这个设置中玩得很好?

采纳答案by randomx

There are few ways to tackle this.

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

Option 1:

选项1:

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

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

$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:

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

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

Option 2:

选项 2:

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

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

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

/system/application/config/config.php,将您的页面设置为:

$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:

有一些更hacky的方法可以使用 header() 重定向等来做到这一点,但我认为您不想为此选项维护不同的代码库。我不建议这样做,但您可以执行以下操作:

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

回答by skyler

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

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

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

$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.

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

回答by Code Prank

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

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

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

回答by NexusRex

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.

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

STEP 1

第1步

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

创建文件: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

第2步

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

在函数中自定义路径,您的站点的哪些区域应该被强制使用 HTTPS。

STEP 3

第 3 步

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

将以下内容添加到 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()
                                );

回答by Jonas WebDev

I can easily, I only define:

我很容易,我只定义:

$config['base_url'] = '';

CI get base_url automaticaly =D

CI 自动获取 base_url =D

回答by Nassim

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

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

$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.comthen $secure_base_url will be https://www.example.com

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

回答by 2114L3

another questionis similar to this

另一个问题与此类似

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

您可以使用与协议相关的 url,它将在 CI 中的链接中保持持久性。

in your config.phpinstead of:

在你的config.php而不是:

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

put;

放;

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

information about protocol-relative links here.

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

回答by Gustavo Coelho

I solved the problem with in config.php

我在 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

目录麻烦,可以用简单的符号链接ln -s

回答by fedmich

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

这对我有用。我们的服务器都有$_SERVER['SERVER_PORT']$_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']

我们应该首先检查$_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']) ;

tested this on our linux server...

在我们的 linux 服务器上对此进行了测试...



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

顺便说一句,它基于斯凯勒之前的回答。

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