In this Tutorial, we will use the CI third-party SSL helper to enforce an https or HTTP URI request and response between CI and the browser. This example will be constructed from the following helpers:

  • application/helpers/ssl_helper.php: The CI helper for SSL that implements SSL on the links.
  • application/controllers/helpersslexample.php: This controller loads the helper and implements SSL on the links. The helper is loaded in the constructor.
  • application/views/helper-ssl-view.php: This is the rendered view that SSL is implemented on.
  • Let us assume the URI to the project root is


    The source code is provided with this book via URLs.

    The helper file

    This CI helper file implements the services described in the preceding section. This helper uses the built-in CI URL library and URL helper using the redirect CI URL helper function.

      <?php if ( ! defined('BASEPATH')) exit('No direct script access  allowed');
      if (!function_exists('force_ssl')) {function force_ssl(){ // get the CI instance to access the CI resources  $CI =& get_instance();// Change the base_url to have https prefix  
          $CI->config->config['base_url'] =str_replace('http://', 'https://',$CI->config->config['base_url']);
                if ($_SERVER['SERVER_PORT'] != 443){     // redirect CI to use https URI
            // so that ($CI->uri->uri_string() return 
            // the current URI with https prefix
      if (!function_exists('remove_ssl')) {function remove_ssl(){$CI =& get_instance();
          // Change the base_url to have http prefix  
          $CI->config->config['base_url'] =str_replace('https://', 'http://',$CI->config->config['base_url']);
          if ($_SERVER['SERVER_PORT'] != 80){// redirect CI to use http URI
            // so that ($CI->uri->uri_string() return 
            // the current URI with http prefix

    The controller file

    Now we will see how the controller loads the SSL helper and calls its function force_ssl to enforce the HTTPS URI request and response with the browser.

    class Helpersslexample extends CI_Controller {
        public function __construct() {
          // Loading the ssl helper
          // Enforce URI request of https 
          * Index Page for this controller.
        public function index()
    /* End of file helpersslexample.php */
    /* Location: ./application/controllers/helpersslexample */