Part of the EllisLab Network
   
2 of 2
2
https for only some views?
Posted: 03 June 2010 02:48 PM   [ Ignore ]   [ # 16 ]  
Summer Student
Total Posts:  8
Joined  05-17-2010

hi everybody

I m little speak english.

im use this way

<?php $base_url = (@$_SERVER['HTTPS'])? preg_replace('/http:/''https:'base_url()) : base_url(); ?> 

example:

this is my header view file
location: ./aplication/view/inc/header.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
html >
<
head>
    <
title><?$title?></title>
    
    <
meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <
meta name="author" content="<?= $this->config->item('author_name').' '.$this->config->item('author_email'); ?>" />
    
    <
meta name="audience" content="all" />
    <
meta name="Robots" content="all" />
    <
meta name="googlebot" content="all" />
    
<?php $base_url = (@$_SERVER['HTTPS'])? preg_replace('/http:/''https:'base_url()) : base_url(); ?>
    
    
<base href="<?= $base_url ?>" />
        
    <!-- 
Css -->
    <
link rel="stylesheet" type="text/css" href="inc/css/style.css" media="screen" />
    <!-- 
//Css -->
    
    
<!-- Javascripts -->
    
[removed]
        
var base_url '<?= base_url() ?>';
        var 
site_url '<?= site_url() ?>';
    
[removed]
    [removed][removed]
    [removed][removed]
    [removed][removed]
    
<!-- //Javascripts -->
        
........................ other lines 

how do you think ?

Profile
 
 
Posted: 16 September 2010 05:24 PM   [ Ignore ]   [ # 17 ]  
Summer Student
Total Posts:  3
Joined  07-27-2010

Hi there…. I just want to share a quick update to nevercraft’s solution.

In force_ssl_helper.php, I’ve added a function to remove SSL if it isn’t needed, as follows:

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());
        
}
    }
}

if ( ! function_exists('remove_ssl'))
{
    
function remove_ssl()
    
{
        $CI 
=& get_instance();
        
$CI->config->config['base_url'str_replace('https://''http://'$CI->config->config['base_url']);
        if (
$_SERVER['SERVER_PORT'!= 80)
        
{
            redirect
($CI->uri->uri_string());
        
}
    }

but where does the page knows to use or not SSL? Simply enough.

Just add the following code to the very top of your page:

<?php
    $this
->load->helper('force_ssl');
    if (
in_array($this->uri->segment(1), $this->config->item('ssl_pages'))){
        force_ssl
();
    
else {
        remove_ssl
();
    
}
?> 

than you just have to create an item in the config file called ‘ssl_pages’ that is a single dimension array with the controller names that you which to force SSL to.


EXAMPLE:

If I have a header file which is called on every view page and a controller named My_secure_checkout, than I’ll have to do this:


1. Create a helper file named force_ssl_helper.php in my application/helpers folder and paste the code I’ve mentioned earlier on this post.

2. On header.php file or whatever you name it, paste the following code to the very top:

<?php
    $this
->load->helper('force_ssl');
    if (
in_array($this->uri->segment(1), $this->config->item('ssl_pages'))){
        force_ssl
();
    
else {
        remove_ssl
();
    
}
?> 

3. On config.php, add the following line to the very end:

$config['ssl_pages'= array('my_secure_checkout'); 

and…. Voilá! Now I have My_secure_checkout controller using SSL, leaving others untouched.

I know this is not the best solution, but it works fine for me. I hope it can help somebody.

 


HAPPY CODING….

Profile
 
 
Posted: 16 November 2010 02:45 AM   [ Ignore ]   [ # 18 ]  
Summer Student
Total Posts:  2
Joined  11-15-2010

force_ssl_helper.php may not be useful when you send an HTTP request with POST method to execute an action(function), which is required SSL.

When redirect() is executed, it sends an GET HTTP request via https to execute the same action, but the data of POST is lost.

There is an example:

class Welcome extends Controller {

    
// use to authenticate a user by email and password
    
function authenticate() {
       force_ssl
();    // this action requires SSL

       
$email $this->input->post('email');
       
$password $this->input->post('password');
       ..... 

When the login form is submitted, browser sends a request using POST method to http://192.168.1.1/index.php/welcome/authenticate, and it is redirected to https://192.168.1.1/index.php/welcome/authenticate at once, because this action requires SSL. Unfortunately, I can not retrieve email and password any more.

Profile
 
 
Posted: 03 February 2011 10:44 AM   [ Ignore ]   [ # 19 ]  
Summer Student
Total Posts:  11
Joined  09-16-2010

I have tried all the suggestions in this thread for Htaccess to remove index.php from Code Igniter URL.
This rewrite rules removes index.php from URL but does not display under Secure URL.
for example:
http://www.mysite.com/CodeIg/MyController —Works perfect

https://www.mysite.com/CodeIg/index.php/MyController —Works perfect

    But

https://www.mysite.com/CodeIg/MyController —Does not Work at all. It gives page not found server error.

Then I tried adding the htaccess rules in Virtual host file…
Now the Screen get displayed but without any css, js and Images included in the page.

All these folder like css, images and JS are in application directory.

Does anyone know why this problem is coming?

REply to post would be appreciated a lot….


Thanks

Profile
 
 
Posted: 19 February 2011 10:15 AM   [ Ignore ]   [ # 20 ]  
Summer Student
Avatar
Total Posts:  24
Joined  08-05-2008

C4iO [PyroDEV] - your solution works the best for me. My two cents is to replace header.php by MY_controller.php:

class MY_Controller extends Controller
{
    
function __construct()
    
{
        parent
::__construct();
    
    
$this->load->helper('force_ssl');
    if (
in_array($this->uri->segment(1), $this->config->item('ssl_pages'))){
        force_ssl
();
    
else {
        remove_ssl
();
    
}

    }

then every controller starts by

class any_controller extends MY_Controller 

this way you check if page shoud be displayed as https in every controller

Profile
 
 
Posted: 18 March 2011 11:12 PM   [ Ignore ]   [ # 21 ]  
Summer Student
Total Posts:  12
Joined  07-08-2010
manisha - 03 February 2011 03:44 PM

I have tried all the suggestions in this thread for Htaccess to remove index.php from Code Igniter URL.
This rewrite rules removes index.php from URL but does not display under Secure URL.
for example:
http://www.mysite.com/CodeIg/MyController —Works perfect

https://www.mysite.com/CodeIg/index.php/MyController —Works perfect

    But

https://www.mysite.com/CodeIg/MyController —Does not Work at all. It gives page not found server error.

Then I tried adding the htaccess rules in Virtual host file…
Now the Screen get displayed but without any css, js and Images included in the page.

All these folder like css, images and JS are in application directory.

Does anyone know why this problem is coming?

REply to post would be appreciated a lot….


Thanks

Look at the rewrite condition and rules in this thread http://codeigniter.com/forums/viewthread/86113/ they solved my problem.

Profile
 
 
Posted: 09 November 2011 01:13 PM   [ Ignore ]   [ # 22 ]  
Grad Student
Rank
Total Posts:  38
Joined  09-10-2009
parrots - 24 June 2008 09: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 
$^(register/payment|login)
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R=301,L]

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

RewriteCond $!^(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.

I’m doing exactly this but I’m getting stuck in a GET loop. The redirect to https happens but CI won’t show me any result….... been at this for hours, don’t know why…

/T

Profile
 
 
Posted: 18 April 2012 06:25 PM   [ Ignore ]   [ # 23 ]  
Summer Student
Total Posts:  13
Joined  11-07-2009

Here’s how I did it in 5 steps:

1. MY_url_helper

/**
* force_ssl
*  secures (https), all assets called w/ base_url_asset()
*  call before view content
*
* @params - url_string - optional, see codeigniter base_url() docs
*/
function force_ssl()
{
    $CI 
=& get_instance();
    
$CI->force_ssl true;
}


/**
* base_url_asset
*  use for asset links that may/may not be secure (depending on the page)
*
* @params - url_string - optional, see codeigniter base_url() docs
*/
function base_url_asset($url_string=false){
 $CI 
=& get_instance();

 if(isset(
$CI->force_ssl)){
  
return str_replace('http''https'base_url($url_string));
 
else return base_url($url_string);
}


/**
* base_url_secure
*  converts a non-secure http base_url to https
*
* @params - url_string - optional, see codeigniter base_url() docs
*/
function base_url_secure($url_string=false)
{
 
return str_replace('http','https',base_url($url_string));

2. Top of my secure view:

<?php force_ssl(); ?> 

3. Link to this secure page (from another view)

<a href="<?php echo base_url_secure();?>registration/create">Create Registration</a

4. Secure all files used on the secure page:

<link rel="stylesheet/less" href="<?php echo base_url_asset();?>webroot/styles.less" type="text/css" /> 

5. Secure the form, in your secure page

<?php echo form_open(base_url_secure('registration'));?> 

base_url_asset - will pull regular base_url() - unless - you declare force_ssl() at the top of your view, then it will switch them.  That’s it!  I’m sure there’s a better way, so please let me know, or edit as needed.

 

 

 

Profile
 
 
   
2 of 2
2