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

[eluser]renownedmedia[/eluser]
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...

Code:
$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();?>" />
#2

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

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

Code:
$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!
#4

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


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

[eluser]renownedmedia[/eluser]
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
#6

[eluser]paulopmx[/eluser]
How about this:

$url = $_SERVER['SCRIPT_NAME'];
$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