Part of the EllisLab Network
   
2 of 5
2
Automatic config[base_url]
Posted: 06 November 2007 05:23 AM   [ Ignore ]   [ # 11 ]  
Grad Student
Rank
Total Posts:  76
Joined  06-17-2007
paulopmx - 04 November 2007 11:02 PM

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?

Profile
 
 
Posted: 25 November 2007 09:42 AM   [ Ignore ]   [ # 12 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  165
Joined  04-23-2007

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]

Profile
 
 
Posted: 06 December 2007 03:08 PM   [ Ignore ]   [ # 13 ]  
Grad Student
Rank
Total Posts:  76
Joined  06-17-2007

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']);

Profile
 
 
Posted: 06 December 2007 05:19 PM   [ Ignore ]   [ # 14 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  133
Joined  09-04-2007

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

Profile
 
 
Posted: 06 December 2007 05:29 PM   [ Ignore ]   [ # 15 ]  
Research Assistant
RankRankRank
Total Posts:  443
Joined  07-04-2007

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

Profile
 
 
Posted: 06 December 2007 05:58 PM   [ Ignore ]   [ # 16 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  165
Joined  04-23-2007

Guys, this Automatic config[base_url] solution is actually useful in letting you use multiple domains or addresses for the same website, not just 2 or 3.

 Signature 

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

Profile
 
 
Posted: 07 December 2007 03:46 AM   [ Ignore ]   [ # 17 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  133
Joined  09-04-2007

@paulopmx: I’m aware of that, but setting up a new website starts with setting the configs anyway so I don’t really care about multiple domains. What I do care about is the problem mentioned in the opening post, which is setting up a site, making a slight config mod and uploading a config file that will not work on that server.

@tonanbarbarian: Nice, although I only compared mine to statements with multiple operators in it.

Profile
 
 
Posted: 18 March 2008 12:38 PM   [ Ignore ]   [ # 18 ]  
Grad Student
Avatar
Rank
Total Posts:  55
Joined  03-09-2008

Please excuse my ignorance, how do you set certain pages to be HTTP while other pages are HTTPS? (for instance, a home page vs vs a enter billing info page)

Profile
 
 
Posted: 18 March 2008 12:51 PM   [ Ignore ]   [ # 19 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  714
Joined  02-05-2007
nikefido - 18 March 2008 12:38 PM
Please excuse my ignorance, how do you set certain pages to be HTTP while other pages are HTTPS? (for instance, a home page vs vs a enter billing info page)

First, be sure all links to secure pages are https. Second, in secure pages verify that the protocol is https. If not, then redirect using https.

It’s helpful to extend a common parent controller for all secure controllers. Then do the protocol check from the constructor of the secure parent controller.

 Signature 

“I am the terror that flaps in the night”

Profile
 
 
Posted: 18 March 2008 12:53 PM   [ Ignore ]   [ # 20 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  161
Joined  02-14-2007

Well depending on the server you would install a SSL certificate for the site - thus when https is used for the domain it knows where to pull the files from. What I’ve done is checks on certain pages so that if the user goes to the page and it’s not https that it will redirect them to the https connection.

 Signature 

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

Profile
 
 
   
2 of 5
2
 
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: 62604 Total Logged-in Users: 26
Total Topics: 77089 Total Anonymous Users: 2
Total Replies: 416305 Total Guests: 175
Total Posts: 493394    
Members ( View Memberlist )
Newest Members:  wengbaoshanGenki1gabewellsGlaucoeudj1nsehartEasyMLance SloanandrewjhscottShuvo