Skip to content

Instantly share code, notes, and snippets.

@aarongarciah
Created January 23, 2016 18:38
Show Gist options
  • Save aarongarciah/f139c10559b39942f438 to your computer and use it in GitHub Desktop.
Save aarongarciah/f139c10559b39942f438 to your computer and use it in GitHub Desktop.
Fix base_url in Codeigniter 3.0 when working on local server.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Config extends CI_Config {
public function __construct()
{
$this->config =& get_config();
// Set the base_url automatically if none was provided
if (empty($this->config['base_url']))
{
// The regular expression is only a basic validation for a valid "Host" header.
// It's not exhaustive, only checks for valid characters.
if (isset($_SERVER['HTTP_HOST']) && preg_match('/^((\[[0-9a-f:]+\])|(\d{1,3}(\.\d{1,3}){3})|[a-z0-9\-\.]+)(:\d+)?$/i', $_SERVER['HTTP_HOST']))
{
$base_url = (is_https() ? 'https' : 'http').'://'.$_SERVER['HTTP_HOST']
.substr($_SERVER['SCRIPT_NAME'], 0, strpos($_SERVER['SCRIPT_NAME'], basename($_SERVER['SCRIPT_FILENAME'])));
}
else
{
$base_url = 'http://localhost/';
}
$this->set_item('base_url', $base_url);
}
log_message('info', 'Config Class Initialized');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment