Welcome Guest, Not a member yet? Register   Sign In
Automatically change $config['base_url'] when site is uploaded

I made the following change to the config.php file so that when I upload a codeigniter file to any web server, it will automatically change the base_url() output. I can't really see a need to ever manually define this anyway...

$config['base_url'] = (isset($_SERVER['HTTPS']) ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . str_replace("index.php", "", $_SERVER['PHP_SELF']);

Anyone have any reasons this is a bad idea? It also works very well with the base href code:

Quote:<base href="<?=base_url();?>" />

[eluser]Thorpe Obazee[/eluser]
$config['base_url']    = ((empty($_SERVER['HTTPS']) OR $_SERVER['HTTPS'] === 'off') ? 'http' : 'https').'://'.$_SERVER['HTTP_HOST'].'/';

[quote author="bargainph" date="1244011796"]
$config['base_url']    = ((empty($_SERVER['HTTPS']) OR $_SERVER['HTTPS'] === 'off') ? 'http' : 'https').'://'.$_SERVER['HTTP_HOST'].'/';

$config['base_url']    = ((empty($_SERVER['HTTPS']) OR $_SERVER['HTTPS'] === 'off') ? 'http' : 'https').'://'.$_SERVER['HTTP_HOST'].str_replace("index.php", "", $_SERVER['PHP_SELF']);

I just derived mine from yours so that it works within sub-directories. Have you had servers report $_SERVER['HTTPS'] as 'off'? Mine have always not had it set when in HTTP!

[eluser]Thorpe Obazee[/eluser]
I don't think PHP_SELF is the correct one to use. It doesn't work for routed uris

Quote: Note: Note that when using ISAPI with IIS, the value will be off if the request was not made through the HTTPS protocol.

everything gets loaded initially through the top level index.php, so it should always work right.

Good call on the IIS stuff, forgot about M$ land lol

How about this:

$url = substr($url,0,strpos($url,".php"));
$url = substr($url,0,(strlen($url) - strpos(strrev($url),"/")));
$url = ((empty($_SERVER['HTTPS']) OR $_SERVER['HTTPS'] === 'off') ? 'http' : 'https')."://".$_SERVER['HTTP_HOST'].$url;

$config['base_url'] = $url;

Will let your app work even if its inside a subfolder, or if its not using index.php as a loader.

Theme © iAndrew 2016 - Forum software by © MyBB