Part of the EllisLab Network
   
5 of 6
5
Automatic config[base_url]
Posted: 07 October 2008 05:28 AM   [ Ignore ]   [ # 41 ]  
Summer Student
Avatar
Total Posts:  22
Joined  06-18-2007

Hi there,

$config[‘base_url’] = “https://”.$_SERVER[‘HTTP_HOST’];

the above works great on intranet, internet and SSL. Thanks! smile

Profile
 
 
Posted: 07 October 2008 07:08 AM   [ Ignore ]   [ # 42 ]  
Summer Student
Total Posts:  6
Joined  01-20-2008
paulopmx - 05 September 2007 02:19 PM

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.


My suggestion:

index.php (the main file of your CI application) :

$_ciroot rtrim(dirname(__FILE__),"/")."/";
if (
stristr(PHP_OS'WIN')) $_ciroot str_replace("\\""/"$_ciroot);
$_ciroot str_replace($_SERVER['DOCUMENT_ROOT']''$_ciroot);
define('CIROOT'$_ciroot ); 

config.php:

$config['base_url']    "http://".$_SERVER['HTTP_HOST'].CIROOT

...you can improve the protocol detection.

BR!

Profile
 
 
Posted: 01 July 2009 03:06 AM   [ Ignore ]   [ # 43 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  169
Joined  04-23-2007

I’ve recently discovered that, sometimes scriptname is incorrectly parsed and includes the slashes after index.php, and when that happens, basename also fails.

So I’ve adapted the script below.

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

should work even if your web app is in a very deep subfolder.

 Signature 

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

Profile
 
 
Posted: 25 September 2009 01:07 PM   [ Ignore ]   [ # 44 ]  
Summer Student
Total Posts:  2
Joined  09-25-2009

Hi i am beginner in php and code igniter. I have a free hosting on which i can’t modify the allow_url_fopen and allow_url_include.

In config i have

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

And i get error:

A PHP Error was encountered

Severity
Warning

Message
: require_once() [function.require-once]URL file-access is disabled in the server configuration

Filename
views/glowna_v.php

Line Number

(as you can see here http://lysiu.pl/wl2)

Can you tell me what value i need to set in base_url to get it work?

TIA

Profile
 
 
Posted: 25 September 2009 02:00 PM   [ Ignore ]   [ # 45 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  804
Joined  06-10-2009

Oh, you shouldn’t be using base_url in an include or library load because base_url includes http://, etc.

Instead use a relative path, such as require_once(”../views/glowna_v.php”), or $this->load->view(‘glowna_v’).

 Signature 

CreativeHalls Web Design and Printing
A few of my projects:
OurGulfCoast Property Management and Vacation Rental (ASP/.NET)
BukuBux - Money Saving Coupons and Gift Certificates (CodeIgniter, LAMP/MySQL)
Rentals800.com - Find a place to rent (CodeIgniter, LAMP/MySQL)
bdh (dot) hall (at) gmail (dotcom)

Profile
 
 
Posted: 25 September 2009 02:56 PM   [ Ignore ]   [ # 46 ]  
Summer Student
Total Posts:  2
Joined  09-25-2009

thanks, on irc channel i have asked the same question and i have ended with this:

$BasePath str_repeat("../"substr_count(dirname($_SERVER["SCRIPT_NAME"]), "/"));
$config['base_url'$BasePath '/wl2/'

and it is working now, thanks

Profile
 
 
Posted: 25 September 2009 03:51 PM   [ Ignore ]   [ # 47 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  2774
Joined  07-27-2006

I use this, because all the logic you guys are doing is already done by the browser.

$config['base_url''/'
 Signature 

Check out the Template Library
Oh yeah, I tweet, too (regarding CodeIgniter on occassion).

Profile
 
 
Posted: 25 September 2009 04:20 PM   [ Ignore ]   [ # 48 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  674
Joined  07-16-2008
$config['base_url''/'

Now that does not work everywhere. It works only if the site is at the root level. When online, that works but on localhost the site rarely is in the root level(I have tons of aliases set up in my localhost).

Profile
 
 
Posted: 25 September 2009 04:23 PM   [ Ignore ]   [ # 49 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3216
Joined  06-10-2007
$config['base_url''/sub-directory/'

What is wrong with you people?

 Signature 

URI Language Identifier | Modular Extensions - HMVC | View Object | Widget plugin | Access Control library

Profile
 
 
Posted: 25 September 2009 04:33 PM   [ Ignore ]   [ # 50 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  2774
Joined  07-27-2006

Haha.. Not to mention there are more elegant ways to work locally than subdir-ing the hell out of localhost. VirtualHost, anyone?

 Signature 

Check out the Template Library
Oh yeah, I tweet, too (regarding CodeIgniter on occassion).

Profile
 
 
   
5 of 6
5