Welcome Guest, Not a member yet? Register   Sign In
From Http to Https - Now browser gives ERR_CONTENT_DECODING_FAILED error in console
#4

But what if i have 10 to 30 controller or more i have to put manually force_ssl(); in each controller constructor,
I have found another solution for this by using hooks.

Just enable hooks from application/config/config.php

Put the following code into application/config/hooks.php

hooks.php

$hook['post_controller_constructor'][] = array(
'function' => 'redirect_ssl',
'filename' => 'ssl.php',
'filepath' => 'hooks'
);

This hook will call the redirect_ssl function in ssl.php file located in application/hooks/ssl.php

ssl.php

function redirect_ssl()
{
$CI =& get_instance();

$class = $CI->router->fetch_class();

// Add more controller name to exclude ssl.
$exclude = array();

if(!in_array($class,$exclude))
{
// redirecting to ssl.
$CI->config->config['base_url'] = str_replace('http://', 'https://', $CI->config->config['base_url']);
if ($_SERVER['SERVER_PORT'] != 443) redirect($CI->uri->uri_string());
}

else
{
// redirecting with no ssl.
$CI->config->config['base_url'] = str_replace('https://', 'http://', $CI->config->config['base_url']);
if ($_SERVER['SERVER_PORT'] == 443) redirect($CI->uri->uri_string());
}
}

So this function will called immediately after your controller is instantiated, but prior to any method calls happening and set the base url according to the condition by default it will set https protocol for all controllers in case you want to exclude a controller from the ssl list just put the name of the class in $exclude array.

And you simply enable/disable the hooks from application/config/config.php file by just change the value of the $config['enable_hooks'] to true/false.

Thanks TPojka once again for your reply it was really useful for me.
Reply


Messages In This Thread
RE: From Http to Https - by Tpojka - 04-03-2016, 06:16 AM
RE: From Http to Https - by MasoodRehman - 04-03-2016, 09:40 AM
RE: From Http to Https - by MasoodRehman - 04-03-2016, 12:38 PM
RE: From Http to Https - by Tpojka - 04-03-2016, 12:59 PM
RE: From Http to Https - by MasoodRehman - 04-04-2016, 12:58 PM



Theme © iAndrew 2016 - Forum software by © MyBB