EllisLab text mark
Advanced Search
1 of 4
1
   
Automatic config[base_url]
Posted: 05 September 2007 11:19 AM
Avatar
Joined: 2007-04-23
167 posts

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

$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.

 Signature 

A Better and more Flexible Paging Solution for CI
Automatic config[base_url]

 
Posted: 05 September 2007 11:40 AM   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2006-07-23
148 posts

good one, thanks smile

 Signature 

running man webdevelopment

 
Posted: 19 September 2007 01:05 PM   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2006-09-13
70 posts

improved one :

$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.

 
Posted: 19 September 2007 05:40 PM   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2007-07-30
2144 posts

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/

 Signature 

Follow me on twitter here.
MichaelWales.com | MichaelWales.info

 
Posted: 19 September 2007 10:44 PM   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2006-03-23
3194 posts

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

 Signature 

DerekAllard.com - CodeIgniter, ExpressionEngine, and the World of Web Design

 
Posted: 20 September 2007 09:07 PM   [ # 5 ]   [ Rating: 0 ]
Joined: 2007-09-10
15 posts
walesmd - 19 September 2007 09:40 PM

.... 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/

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 )

//| 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

 Signature 

... me write good php code ...

 
Posted: 09 October 2007 10:41 AM   [ # 6 ]   [ Rating: 0 ]
Avatar
Joined: 2007-02-14
95 posts

I use this pretty successfully

$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
 Signature 

Trying to do it right the first time! whoops guess that didn’t work out so well…

 
Posted: 25 October 2007 06:39 PM   [ # 7 ]   [ Rating: 0 ]
Joined: 2007-06-17
51 posts

Maybe this sort of default value to enable autoconfiguration will make its way into the next release of CI.

 
Posted: 27 October 2007 10:29 PM   [ # 8 ]   [ Rating: 0 ]
Avatar
Joined: 2007-06-11
2987 posts
phpMaster - 21 September 2007 01:07 AM
//| 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.

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.

 Signature 

————————
Blog | Twitter | GitHub | BitBucket
————————-
PyroCMS - open source modular CMS built with CodeIgniter
PancakeApp - Simple, hosted invoicing/w project management

 
Posted: 28 October 2007 02:21 AM   [ # 9 ]   [ Rating: 0 ]
Avatar
Joined: 2007-04-23
167 posts
phpMaster - 21 September 2007 01:07 AM
walesmd - 19 September 2007 09:40 PM

.... 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/

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 )

//| 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

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’].

 Signature 

A Better and more Flexible Paging Solution for CI
Automatic config[base_url]

 
Posted: 05 November 2007 12:02 AM   [ # 10 ]   [ Rating: 0 ]
Avatar
Joined: 2007-04-23
167 posts

Wiki Page

http://codeigniter.com/wiki/Automatic_configbase_url/

 Signature 

A Better and more Flexible Paging Solution for CI
Automatic config[base_url]

 
Posted: 06 November 2007 06:23 AM   [ # 11 ]   [ Rating: 0 ]
Joined: 2007-06-17
51 posts
paulopmx - 05 November 2007 05:02 AM

Wiki Page

http://codeigniter.com/wiki/Automatic_configbase_url/

Thank you.  grin
However, do you want to add also the part to work with HTTP and HTTPS?

 
Posted: 25 November 2007 10:42 AM   [ # 12 ]   [ Rating: 0 ]
Avatar
Joined: 2007-04-23
167 posts

Sure, haven’t had much time though I think someone replied with a solution for the http and https part, i will incorporate it later, but if you want you can edit the wiki yourself.

 Signature 

A Better and more Flexible Paging Solution for CI
Automatic config[base_url]

 
Posted: 06 December 2007 04:08 PM   [ # 13 ]   [ Rating: 0 ]
Joined: 2007-06-17
51 posts

Updated the wiki cause i needed this. Just in case, here the code:

$config['base_url'= ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'== "on") ? "https" "http");
$config['base_url'.= "://".$_SERVER['HTTP_HOST'];
$config['base_url'.= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']); 
 
Posted: 06 December 2007 06:19 PM   [ # 14 ]   [ Rating: 0 ]
Avatar
Joined: 2007-09-04
118 posts

Now the main purpose of this post is to finally use the ‘code’ tag here grin , but here’s the way I like it and I’m almost sure that I even win microseconds and 0.00001% readablity…

if ($_SERVER['HTTP']=='localhost'$config['base_url''http://localhost/path/to/local/folder/';
else 
$config['base_url''http://www.server.tld/path/to/online/folder/'

Okay, I know I need to set it every time I create a new project… smile

 
Posted: 06 December 2007 06:29 PM   [ # 15 ]   [ Rating: 0 ]
Joined: 2007-07-05
656 posts

actually the ternary operator x ? y : z; is faster than an if statement

so use this and it is even faster

$config['base_url'= ($_SERVER['HTTP']=='localhost') ? 
  
'http://localhost/path/to/local/folder/' // local base url
  
'http://www.server.tld/path/to/online/folder/'// live base url 

maybe not quite as readable however although the commenting helps

 
1 of 4
1