• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Automatic config[base_url]

#1
[eluser]paulopmx[/eluser]
Don’t you just hate it, when you move from development server to production server that you have to change the $config[’base_url’] setting?

Well I do. Because I do it a lot, specially when I’m demoing a web application, that’s why I added this code to the config.php
Code:
$root = "http://".$_SERVER['HTTP_HOST'];
$root .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);

$config['base_url']    = "$root";

Once thats added, Codeigniter base_url will adapt to whatever the url you're using.

#2
[eluser]frenzal[/eluser]
good one, thanks Smile

#3
[eluser]Dready[/eluser]
improved one :

Code:
$config['base_url'] = "http://".$_SERVER['HTTP_HOST'];
$config['base_url'] .= preg_replace('@/+$@','',dirname($_SERVER['SCRIPT_NAME'])).'/';

makes use of dirname and takes care of having only one trailing slash.

#4
[eluser]Michael Wales[/eluser]
better one:
take the 3.6 seconds to change the one variable in config.php

Just kidding guys - nice addition that has tripped me up many times before. I've been completely stumped wondering why my app wasn't working properly only to find it was 17 little characters destroying my life: http://localhost/

#5
[eluser]Derek Allard[/eluser]
3.6 seconds to type that Michael. Dude, you need to optimize yourself. I'm down to 3.2 seconds. And just to show you how fast CodeIgniter is, for me to make the exact change in CakePHP took 5.2 seconds! Truly CI is fast!

Smile

#6
[eluser]phpMaster[/eluser]
[quote author="walesmd" date="1190252421"].... nice addition that has tripped me up many times before.
I've been completely stumped wondering why my app wasn't working properly
only to find it was 17 little characters destroying my life: http://localhost/[/quote]

Hi!
I posted a 'mini article' in another topic, about same issue:
http://ellislab.com/forums/viewreply/297477/

In a bit shorter version:
--------------------------------------------------------------------------

Many many many many softwares have not an intelligent way of setting BASE-URL.
I make 99% of my PHP work and applications admin from http://localhost/
Now, if I set base-url in a software (sometimes even in the database!)
like this: http://localhost/app/
then nobody else but me can use my app.

If I set base-url like this: http://www.mydomain.com/app/
then I can not admin my apps (unless I connect via some proxy server)

This is how I have set config of my CodeIgniter ( and a whole bunch of other less intelligent softwares Smile )
Code:
//| URL to your CodeIgniter root.  WITH a trailing slash:

$config['base_url']    = "http://".$_SERVER['HTTP_HOST']."/codeigniter/";
Adding a couple of more lines, it is even possible to detect if it should be either of:
https:// http:// ... and the phpBB3 developers even auto detect whatever :8080 port number!

This way it will work whatever URL alias is used to visit your site!
yourdomain.com, localhost, 127.0.0.1, 192.168.0.1 etc etc
And if you change your domain name, you wont have to change a thing.
--------------------------------------------------------------------------

Regards - phpMaster

#7
[eluser]Code Arachn!d[/eluser]
I use this pretty successfully

Code:
$proto = "http" .
    ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "s" : "") . "://";
$server = isset($_SERVER['HTTP_HOST']) ?
    $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
$config['base_url']    = $proto . $server;

#8
[eluser]WolfgangA[/eluser]
Maybe this sort of default value to enable autoconfiguration will make its way into the next release of CI.

#9
[eluser]Phil Sturgeon[/eluser]
[quote author="phpMaster" date="1190351273"]
Code:
//| URL to your CodeIgniter root.  WITH a trailing slash:

$config['base_url']    = "http://".$_SERVER['HTTP_HOST']."/codeigniter/";
Adding a couple of more lines, it is even possible to detect if it should be either of:
https:// http:// ... and the phpBB3 developers even auto detect whatever :8080 port number!

This way it will work whatever URL alias is used to visit your site!
yourdomain.com, localhost, 127.0.0.1, 192.168.0.1 etc etc
And if you change your domain name, you wont have to change a thing.[/quote]

ONLY if you are working in the same directory on both local and live, which you probably wont be. Its a good method (used it myself) but not perfect.

#10
[eluser]paulopmx[/eluser]
[quote author="phpMaster" date="1190351273"][quote author="walesmd" date="1190252421"].... nice addition that has tripped me up many times before.
I've been completely stumped wondering why my app wasn't working properly
only to find it was 17 little characters destroying my life: http://localhost/[/quote]

Hi!
I posted a 'mini article' in another topic, about same issue:
http://ellislab.com/forums/viewreply/297477/

In a bit shorter version:
--------------------------------------------------------------------------

Many many many many softwares have not an intelligent way of setting BASE-URL.
I make 99% of my PHP work and applications admin from http://localhost/
Now, if I set base-url in a software (sometimes even in the database!)
like this: http://localhost/app/
then nobody else but me can use my app.

If I set base-url like this: http://www.mydomain.com/app/
then I can not admin my apps (unless I connect via some proxy server)

This is how I have set config of my CodeIgniter ( and a whole bunch of other less intelligent softwares Smile )
Code:
//| URL to your CodeIgniter root.  WITH a trailing slash:

$config['base_url']    = "http://".$_SERVER['HTTP_HOST']."/codeigniter/";
Adding a couple of more lines, it is even possible to detect if it should be either of:
https:// http:// ... and the phpBB3 developers even auto detect whatever :8080 port number!

This way it will work whatever URL alias is used to visit your site!
yourdomain.com, localhost, 127.0.0.1, 192.168.0.1 etc etc
And if you change your domain name, you wont have to change a thing.
--------------------------------------------------------------------------

Regards - phpMaster[/quote]

But what if your app is in a subdirectory, maybe even a different subdirectory than the one you used in your development server. That's why you need to consider the location of your main index.php in relation with the $_SERVER['HTTP_HOST'].


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.