Part of the EllisLab Network
   
1 of 5
1
Automatic config[base_url]
Posted: 05 September 2007 09:19 AM   [ Ignore ]  
Lab Assistant
Avatar
RankRank
Total Posts:  169
Joined  04-23-2007

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]

Profile
 
 
Posted: 05 September 2007 09:40 AM   [ Ignore ]   [ # 1 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  320
Joined  07-23-2006

good one, thanks smile

 Signature 

running man webdesign

Profile
 
 
Posted: 19 September 2007 11:05 AM   [ Ignore ]   [ # 2 ]  
Grad Student
Avatar
Rank
Total Posts:  98
Joined  09-13-2006

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.

Profile
 
 
Posted: 19 September 2007 03:40 PM   [ Ignore ]   [ # 3 ]  
Moderator
Avatar
RankRankRankRank
Total Posts:  2139
Joined  07-30-2007

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
MichaelWales.com

Profile
 
 
Posted: 19 September 2007 08:44 PM   [ Ignore ]   [ # 4 ]  
Administrator
Avatar
RankRankRankRankRankRank
Total Posts:  6905
Joined  03-23-2006

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
BambooInvoice - Open Source, CodeIgniter powered invoicing.

Profile
MSG
 
 
Posted: 20 September 2007 07:07 PM   [ Ignore ]   [ # 5 ]  
Summer Student
Total Posts:  15
Joined  09-10-2007
walesmd - 19 September 2007 03: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://codeigniter.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 ...

Profile
 
 
Posted: 09 October 2007 08:41 AM   [ Ignore ]   [ # 6 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  162
Joined  02-14-2007

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…

Profile
 
 
Posted: 25 October 2007 04:39 PM   [ Ignore ]   [ # 7 ]  
Grad Student
Rank
Total Posts:  76
Joined  06-17-2007

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

Profile
 
 
Posted: 27 October 2007 08:29 PM   [ Ignore ]   [ # 8 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1553
Joined  06-11-2007
phpMaster - 20 September 2007 07:07 PM
//| 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 | Screencasts
—————————-
REST implementation for CodeIgniter
—————————-
PyroCMS: Demo | Code | Bugs
Libraries: Asset, cURL, CLI, GitHub API, Layout

Profile
 
 
Posted: 28 October 2007 12:21 AM   [ Ignore ]   [ # 9 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  169
Joined  04-23-2007
phpMaster - 20 September 2007 07:07 PM
walesmd - 19 September 2007 03: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://codeigniter.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]

Profile
 
 
Posted: 04 November 2007 11:02 PM   [ Ignore ]   [ # 10 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  169
Joined  04-23-2007

Wiki Page

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

 Signature 

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

Profile
 
 
   
1 of 5
1
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 719, on June 06, 2008 10:16 AM
Total Registered Members: 77497 Total Logged-in Users: 26
Total Topics: 101522 Total Anonymous Users: 1
Total Replies: 544265 Total Guests: 205
Total Posts: 645787    
Members ( View Memberlist )