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:
$this->load->helper('my_url_helper');
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.

Comments

  1. This post helped me out.

    https://stackoverflow.com/questions/20415025/how-to-set-dynamic-base-url-to-https-in-codeigniter/20416463#20416463

    ReplyDelete

Post a Comment

Popular Posts