Wednesday, April 1, 2015

How To Set Base URL To HTTPS in CodeIgniter?

If you need the secure base url i.e. HTTPS URL, you can create helper file to manage it.
We will create secure version of base url function. For this we will create a helper file 'MY_url_helper.php' and save it in 'system/application/helpers'.
Then, open the MY_url_helper.php file (system/application/helpers/MY_url_helper.php) and add the following codes.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if (! function_exists('secure_base_url'))
    function secure_base_url()
        if ($_SERVER["SERVER_PORT"] != 443)
            return str_replace("http://", "https://" , base_url());

/* End of file MY_url_helper.php */
/* Location: ./application/helpers/MY_url_helper.php */

You can load the helper and print the secure base url as follows:
echo secure_base_url();

//output : https://localhost:8888/myProject

Now we have secured versions of base url function. You may now use it when you need https secure base url.

1 comment:

  1. This post helped me out.