Thursday, June 22, 2017

Set dynamic base url to https in CodeIgniter PHP framework

In CodeIgniter framework you need to load the base url form the config file. Generally we do this manually like the following line in the local server.
$config['base_url'] = 'http://localhost/sitename/';
But if you upload your project in the real server you need to change this path. We have some tricks to avoid this confection. If you use some tricks you do not need to change it again. Just replace any of the following code. In your config/config.php, try this: Method one:
$config['base_url'] = "http://".$_SERVER['HTTP_HOST'];
$config['base_url'] .= preg_replace('@/+$@','',dirname($_SERVER['SCRIPT_NAME'])).'/';
Method two:
$root=(isset($_SERVER['HTTPS']) ? "https://" : "http://").$_SERVER['HTTP_HOST'];
$root.= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
$config['base_url'] = $root;

How to practice tutorials…

  • Make a team with your friends (Member should be 4/5).
  • Start to practice one tutorial series along with them.
  • Don’t try to collect source code. Type the code while watching the tutorial.
  • If you face any problem, discuss with team members to solve quickly.