Welcome Guest, Not a member yet? Register   Sign In
SSL in CodeIgnitor
#1

[eluser]got 2 doodle[/eluser]
First off, if you are trying to implement SSL in CodeIgnitor here are a few existing threads.
SSL, how to
https for only some views
problems with ssl in CI

None of these solutions seemed to work the way I thought they should.

modify .htaccess was too specific
force_ssl() changed all of my anchor() by modifying the base url

For reference here is the force_ssl() helper - thanks to nevercraft for this one.

Code:
/*
| Simply call force_ssl() from within any controller method (or the constructor).  
| The user will be redirected to https:// if needed.  
| Also, https:// will show up correctly on any of the other URL helpers used AFTER force_ssl() is called.
*/
if ( ! function_exists('force_ssl'))
{
    function force_ssl($controller)
    {
        $CI =& get_instance();
        $CI->config->config['base_url'] = str_replace('http://', 'https://', $CI->config->config['base_url']);
        if ($_SERVER['SERVER_PORT'] != 443)
        {
            redirect($CI->uri->uri_string());
        }
    }
}

In my case I wanted only some anchors to point to a secure page, so why not define a secure_base_url as well as a base_url.

This approach should work with shared ssl but it hasn't been tested at all. I was hoping some of the more experienced folk could comment on this approach pros and especially cons.

Here's how it works

modify /system/application/config/config.php
Code:
|--------------------------------------------------------------------------
| Base Site URL
|--------------------------------------------------------------------------
|
| URL to your CodeIgniter root. Typically this will be your base URL,
| WITH a trailing slash:
|
|    http://www.your-site.com/
|
*/
//$config['base_url']    = "http://localhost/local_folder/";
$config['base_url'] = "http://".$_SERVER['SERVER_NAME'];
$config['base_url'] .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);

//$config['secure_base_url']    = "http://localhost/local_folder/";
$config['secure_base_url'] = "https://".$_SERVER['SERVER_NAME'];
$config['secure_base_url'] .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);

extend config library - create the following file /system/application/libraries/MY_Config.php

Code:
if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Config extends CI_Config {

    /**
     * Secure Site URL
     *
     * @access    public
     * @param    string    the URI string
     * @return    string
     */        
    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;
        }
    }
    
}



continued in next post


Messages In This Thread
SSL in CodeIgnitor - by El Forum - 11-18-2008, 12:48 PM
SSL in CodeIgnitor - by El Forum - 11-18-2008, 12:49 PM
SSL in CodeIgnitor - by El Forum - 11-18-2008, 12:51 PM
SSL in CodeIgnitor - by El Forum - 11-18-2008, 12:53 PM
SSL in CodeIgnitor - by El Forum - 11-19-2008, 06:54 PM
SSL in CodeIgnitor - by El Forum - 11-20-2008, 09:10 AM
SSL in CodeIgnitor - by El Forum - 12-02-2008, 09:14 AM
SSL in CodeIgnitor - by El Forum - 12-11-2008, 02:16 PM
SSL in CodeIgnitor - by El Forum - 01-12-2009, 06:57 PM
SSL in CodeIgnitor - by El Forum - 01-12-2009, 09:57 PM
SSL in CodeIgnitor - by El Forum - 01-13-2009, 10:04 AM
SSL in CodeIgnitor - by El Forum - 01-17-2010, 08:49 PM
SSL in CodeIgnitor - by El Forum - 02-03-2011, 08:31 AM
SSL in CodeIgnitor - by El Forum - 02-04-2011, 06:19 AM
SSL in CodeIgnitor - by El Forum - 03-10-2011, 12:17 AM



Theme © iAndrew 2016 - Forum software by © MyBB