I’m sure there are lots of resources for this, but here is a fairly robust one (WARNING: Don’t do what Mohammed did! I’ll tell you why in a second):
http://sajjadhossain.com/2008/10/27/ssl-https-urls-and-codeigniter/
So, what’s wrong with the approach? He’s editing core files which is a big no no. He should be extending the functionality of the core files. For example, here are the two files I now have in my site:
/application/helpers/MY_url_helper.php
/application/libraries/MY_Config.php
Then, I placed the following code in each of those files, respectively:
MY_url_helper.php
<?php
if( ! function_exists('secure_site_url') )
{
function secure_site_url($uri = '')
{
$CI =& get_instance();
return $CI->config->secure_site_url($uri);
}
}
if( ! function_exists('secure_base_url') )
{
function secure_base_url()
{
$CI =& get_instance();
return $CI->config->slash_item('secure_base_url');
}
}
if ( ! function_exists('secure_anchor'))
{
function secure_anchor($uri = '', $title = '', $attributes = '')
{
$title = (string) $title;
if ( ! is_array($uri))
{
$secure_site_url = ( ! preg_match('!^\w+://! i', $uri)) ? secure_site_url($uri) : $uri;
}
else
{
$secure_site_url = secure_site_url($uri);
}
if ($title == '')
{
$title = $secure_site_url;
}
if ($attributes != '')
{
$attributes = _parse_attributes($attributes);
}
return '<a href="'.$secure_site_url.'">'.$title.'</a>';
}
}
if ( ! function_exists('secure_redirect'))
{
function secure_redirect($uri = '', $method = 'location', $http_response_code = 302)
{
switch($method)
{
case 'refresh' : header("Refresh:0;url=".secure_site_url($uri));
break;
default : header("Location: ".secure_site_url($uri), TRUE, $http_response_code);
break;
}
exit;
}
}
if (! function_exists('force_ssl'))
{
function force_ssl()
{
if ($_SERVER["SERVER_PORT"] != 443)
{
redirect(str_replace("http://", "https://" , current_url()), "refresh");
}
}
}
MY_Config.php
<?php
class MY_Config extends CI_Config {
function MY_Config()
{
parent::CI_Config();
}
function secure_site_url($uri = '')
{
if (is_array($uri))
{
$uri = implode('/', $uri);
}
if ($uri == '')
{
return $this->slash_item('secure_base_url').$this->item('index_page');
}
else
{
$suffix = ($this->item('url_suffix') == FALSE) ? '' : $this->item('url_suffix');
return $this->slash_item('secure_base_url').$this->slash_item('index_page').preg_replace("|^/*(.+?)/*$|", "\\1", $uri).$suffix;
}
}
}
Now, that was easy, wasn’t it? Go forth and do awesome stuff with this.
