2009-09-30 48 views
20

如何让CodeIgniter使用SSL加载特定页面?我有一个apache2/mode_ssl服务器。 mod_ssl使用与非安全页面不同的文档根目录。例如,https(端口443)将服务于/var/www/ssl_html/以外的页面并且http(端口80)服务于/var/www/html/中的页面。我怎样才能让CodeIgniter和这个设置一起玩呢?如何让CodeIgniter使用SSL加载特定页面?

回答

16

有几种方法可以解决这个问题。

选项1:

我可能会部署到这两个文件夹,然后在文件中的代码:/system/application/config/config.php,设定你的网页:

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

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

然后在您的非SSL虚拟主机文件夹,设置你的配置重定向保护的页面被夹到SSL网站:

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

选项2:

发送一切SSL,并保持所有的代码在一个文件夹中

/system/application/config/config.php,设置你的页面:

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

其他选项

还有一些hacky方法可以通过header()重定向等方式做到这一点,但我不认为你想为这个选项维护不同的代码库。我不建议这样做,但你可以这样做:

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

谢谢。我想我会选择第一个选项,然后重写url_help来处理https。 – ammonkc 2009-09-30 21:06:11

+1

简单的答案是将''config ['base_url']''设置为空字符串,就像在''=''中那样 - CodeIgniter会根据URL自动检测所有的东西。因此,只需输入URL,就可以随意打开任何带或不带SSL的页面。无需在服务器上的ssl和non_ssl目录之间拆分内容。 – f055 2014-03-20 22:19:33

+0

只是FYI,这个'$ config ['base_url'] =“http://”。 $ _SERVER ['http_host']。 “/”;'错了,应该是'$ config ['base_url'] \t ='https://'。 $ _SERVER [“HTTP_HOST”]。 '/';'语法错误,$ _SERVER [“http_host”](小写)不存在... – 2014-03-25 03:24:37

2

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

STEP 1

创建文件:系统/应用/钩/ 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

自定义自己网站的区域应该被强迫在功能路径使用HTTPS。

步骤3

添加以下系统/应用/配置/ hooks.php

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

在应用/配置/配置。PHP,设置BASE_URL到:

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

这将允许它在任何领域,如果你在本地测试是方便工作。

+0

我将此成功地用于: RewriteCond%{HTTPS} = off RewriteRule^my-secure-url-segment https://%{HTTP_HOST}%{REQUEST_URI} [R = 301,L] 在我的.htaccess文件中。 – prograhammer 2013-06-05 21:45:12

+0

没有为我工作,$ _SERVER ['SERVER_PORT']对我来说是80; HTTP_X_FORWARDED_PORT是443 – fedmich 2013-10-22 04:11:49

6

我把下面的行./application/config/config.php文件,它完美的作品:

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

我在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']; 

目录麻烦解决了这个问题, ,你可以使用简单的simbolic链接ln -s

0

这是为我工作的。 我们的服务器都具有$ _ 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']}/"; 
+0

如果CI部署在子目录中,以前的答案不会保留script_name,我们需要保留这一点。 str_replace(basename($ _ SERVER ['SCRIPT_NAME']),“”,$ _ SERVER ['SCRIPT_NAME']) – fedmich 2013-10-22 04:26:50

2

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

$config['base_url'] = ''; 

CI GET BASE_URL automaticaly = d

+0

不再推荐,仅供参考。 – twistedpixel 2016-04-20 11:27:12

+0

是的,CI3在config.php中说“不能用于生产!” – 2114L3 2016-12-12 10:13:06

0

是来到我的脑海的解决方案是使用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

0

另一question与此类似

可以使用相对协议网址这将保持持续的连接在CI。

中,而不是你的config.php

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

放;

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

有关协议相关链接的信息here

相关问题