Part of the EllisLab Network
   
 
https for only some views?
Posted: 23 June 2008 06:48 PM   [ Ignore ]  
Summer Student
Avatar
Total Posts:  11
Joined  05-30-2008

I want to specify a secure connection for only the payment parts of my site, I don’t want to use absolute links in my views, but the base_url is set to the normal http:// server and so makes all links unsecured. Is there any way to specify a secure connection for certain pages?

Profile
 
 
Posted: 23 June 2008 11:33 PM   [ Ignore ]   [ # 1 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  178
Joined  09-12-2007

I am not sure, but you may need multiple CI installations. Is there a problem with having your whole site under https? Let the http root redirect to the https version… If you use cookies, you may also have problems because those are technically different domains (I think, not sure, can someone correct me?)

 Signature 

Voltamp Media
Web: PHP, MySQL, PERL, HTML, CSS, Python, Javascript
Linux: FreeBSD, OpenVPN, SMB, SVN, C, Shell

Profile
 
 
Posted: 24 June 2008 01:03 AM   [ Ignore ]   [ # 2 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  837
Joined  02-05-2007

I’d just change the base_url on those pages that require ssl. See the user guide about setting config items. Also, do a check that https is on for those secure pages and if not, then redirect back using https.

 Signature 

“I am the terror that flaps in the night”

Profile
 
 
Posted: 24 June 2008 01:27 AM   [ Ignore ]   [ # 3 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4839
Joined  07-14-2006

You don’t need multiple CI installations but 2 applications. The way i would do it is to create a codeigniter directory above the public and secure root. put the system and 2 application directories in the codeigniter directory. Name the application directories public and secure. and in the bootstrap files the system directory links to the codigniter/system directory and the application directories are added in relation to the directory the bootstrap file is in.

From that time on you can create your applications accordingly. If you are only going to use the secure application for a few pages you will have only one controller.
I guess you are going to share quite a few things between the public and secure site because the pages should have a consistent layout and use the same data. This will require some hacks as CI doesn’t supports flexible paths.

For views that you put in the added system/views directory you can temporarily switch the path using following methods put in the MY_Loader.php file in both application directories (it should be in the system directory as well but again it due to the non flexible file paths)

class MY_Loader extends CI_Loader
{
var _prev_ci_view_path = '';

function
set_view_path($path)
{
   
if(is_dir($path))
   
{
      $this
->_prev_ci_view_path = $this->_ci_view_path;
      
$this->_ci_view_path = $path;
   
}
}

function reset_view_path()
{
    
if($this->_prev_ci_view_path != '')
    
{
        $this
->_ci_view_path = $this->_prev_ci_view_path;
        
$this->_prev_ci_view_path = '';
    
}
}

}
// usage
$this->load->set_view_path(BASEPATH.'views/');
$this->load->view('someview');
$this->load->reset_view_path();

If you want this to work in php4 you have to add an hack in the system/codeigniter/CodeIgniter.php file.  Look for the hack on the forum; extend loader php4, will get you to the post i think.

For the data you can create a shared model in the added system/models directory where you put the methods needed by the secure and public part of your site and include it in the child models in the application directories.

I hope this will set you on your way smile

Profile
 
 
Posted: 24 June 2008 02:41 AM   [ Ignore ]   [ # 4 ]  
Summer Student
Avatar
Total Posts:  11
Joined  05-30-2008

Thanks for the help guys, I will try those solutions.

Profile
 
 
Posted: 24 June 2008 07:15 AM   [ Ignore ]   [ # 5 ]  
Grad Student
Avatar
Rank
Total Posts:  49
Joined  09-28-2007

I did it by having my .htaccess file force SSL for specific URLs (payment and login).  It saved me from having to muck around with changing the base_url or anything like that:

RewriteEngine on

RewriteCond
%{SERVER_PORT} 80
RewriteCond
$1 ^(register/payment|login)
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R=301,L]

RewriteCond %{SERVER_PORT} 443
RewriteCond
$1 !^(register/payment|images|css|javascript|login)
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301,L]

RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]

As a bonus this ensures that even if someone tries to go to my payment or login page without SSL it will redirect them to the HTTPS version.  It’s worked for me pretty well so far.

Profile
 
 
Posted: 24 June 2008 08:11 AM   [ Ignore ]   [ # 6 ]  
Summer Student
Avatar
Total Posts:  11
Joined  05-30-2008
parrots - 24 June 2008 07:15 AM

I did it by having my .htaccess file force SSL for specific URLs (payment and login).  It saved me from having to muck around with changing the base_url or anything like that:

RewriteEngine on

RewriteCond
%{SERVER_PORT} 80
RewriteCond
$1 ^(register/payment|login)
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R=301,L]

RewriteCond %{SERVER_PORT} 443
RewriteCond
$1 !^(register/payment|images|css|javascript|login)
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301,L]

RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]

As a bonus this ensures that even if someone tries to go to my payment or login page without SSL it will redirect them to the HTTPS version.  It’s worked for me pretty well so far.

Thats a pretty sweet solution, thanks!

Profile
 
 
Posted: 24 June 2008 08:23 PM   [ Ignore ]   [ # 7 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  2915
Joined  07-27-2006

parrots’ solution is what has always worked best for me. Also, I’m always setting base_url to ‘/’

Essentially what you’re doing with mod_rewrite is creating SSL entry and exit points, which I think is most elegant. Good luck with it.

 Signature 

Check out the Template Library
Oh yeah, I tweet, too (regarding CodeIgniter on occassion).

Profile
 
 
Posted: 04 September 2008 11:07 PM   [ Ignore ]   [ # 8 ]  
Summer Student
Total Posts:  17
Joined  12-30-2007

Although this thread is a few months old, I thought I’d share the way I accomplished this with just a simple helper function.

if ( ! function_exists('force_ssl'))
{
    
function force_ssl()
    
{
        $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());
        
}
    }
}

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.

Profile
 
 
Posted: 05 September 2008 07:46 AM   [ Ignore ]   [ # 9 ]  
Summer Student
Total Posts:  19
Joined  08-27-2008

I use a solution like parrot’s as well. In case it’s helpful to see another example, this is the .htaccess file I used on my last ecommerce project.

RewriteEngine on

RewriteBase
/

# Redirect all domain name variations to main site
RewriteCond %{HTTP_HOST} ^site.com [NC]
RewriteRule
^(.*)$ http://www.site.com/$1 [L,R=301]

# Force checkout to be secure
RewriteCond %{SERVER_PORT} 80
RewriteCond
$1 !^(index\.php|images|javascripts|site|admin|stylesheets|robots\.txt)
RewriteRule /checkout(.*)$ https://www.site/checkout$1 [L]

# Force admin to be secure
RewriteCond %{SERVER_PORT} 80
RewriteCond
$1 !^(index\.php|images|javascripts|site|checkout|stylesheets|robots\.txt)
RewriteRule /admin(.*)$ https://www.site.com/admin$1 [L]

# Flip back to http unless in checkout or admin
RewriteCond %{SERVER_PORT} !80
RewriteCond
$1 !^(index\.php|images|javascripts|checkout|admin|stylesheets|robots\.txt)
RewriteRule ^(.*)$ http://www.site.com/$1 [L]

# Get CodeIgniter going
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond
%{REQUEST_FILENAME} !-d
RewriteRule
^(.*)$ index.php/$1
 Signature 

PHP Screencast Tutorials

Profile
 
 
Posted: 06 October 2008 05:20 PM   [ Ignore ]   [ # 10 ]  
Summer Student
Avatar
Total Posts:  3
Joined  07-02-2008
nevercraft - 04 September 2008 11:07 PM

Although this thread is a few months old, I thought I’d share the way I accomplished this with just a simple helper function.

if ( ! function_exists('force_ssl'))
{
    
function force_ssl()
    
{
        $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());
        
}
    }
}

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.

Thanks, nevercraft - this worked perfectly. While the .htaccess solution is viable - it is not very dynamic

Profile
 
 
Posted: 19 January 2009 01:25 PM   [ Ignore ]   [ # 11 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  524
Joined  03-13-2008

thanks nevercraft, force_ssl() is now a permanent addition to my helpers collection.

 Signature 

:wq

Profile
 
 
Posted: 22 March 2009 02:03 PM   [ Ignore ]   [ # 12 ]  
Summer Student
Total Posts:  1
Joined  03-22-2009
nevercraft - 04 September 2008 11:07 PM

Although this thread is a few months old, I thought I’d share the way I accomplished this with just a simple helper function.

if ( ! function_exists('force_ssl'))
{
    
function force_ssl()
    
{
        $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());
        
}
    }
}

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.

The function works perfectly although at the beginning I had some problems because I didn’t have ‘http://’ in my base_url. Also I was using .htaccess to get rid of index.php from url’s and this function was redirecting using index.php, all I had to do was to set $config[‘index_page’] from config.php so it was blank. Maybe it will help someone.

Profile
 
 
Posted: 26 August 2009 11:27 AM   [ Ignore ]   [ # 13 ]  
Summer Student
Avatar
Total Posts:  17
Joined  02-19-2009
Mahtar - 22 March 2009 02:03 PM
nevercraft - 04 September 2008 11:07 PM

Although this thread is a few months old, I thought I’d share the way I accomplished this with just a simple helper function.

if ( ! function_exists('force_ssl'))
{
    
function force_ssl()
    
{
        $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());
        
}
    }
}

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.

The function works perfectly although at the beginning I had some problems because I didn’t have ‘http://’ in my base_url. Also I was using .htaccess to get rid of index.php from url’s and this function was redirecting using index.php, all I had to do was to set $config[‘index_page’] from config.php so it was blank. Maybe it will help someone.


Thanks, that did help me as well.


Also, please note, you must be using the URL helper as well because force_ssl() calls the redirect() function. Either in your controller or in your autoload config file.

$this->load->helper(‘url’);

Profile
 
 
Posted: 13 October 2009 03:41 AM   [ Ignore ]   [ # 14 ]  
Summer Student
Total Posts:  3
Joined  08-26-2007

For an alternate solution to this - using a customised Config class and hook - check out this thread:
http://codeigniter.com/forums/viewthread/131918/

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 721, on January 06, 2010 09:38 AM
Total Registered Members: 114968 Total Logged-in Users: 56
Total Topics: 122423 Total Anonymous Users: 1
Total Replies: 647238 Total Guests: 472
Total Posts: 769661    
Members ( View Memberlist )