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

This is what I use:

$config['base_url'] = rtrim(dirname($_SERVER['SCRIPT_NAME']), '/') . '/';

Why do you need to include the domain? If you just use a relative (relative server?) path, you don't need to worry about the domain or the protocol.

Hi there,

Quote:$config['base_url'] = "https://".$_SERVER['HTTP_HOST'];

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

[eluser]Velizar N.[/eluser]
[quote author="paulopmx" date="1189019955"]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.[/quote]

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['base_url']    = "http://".$_SERVER['HTTP_HOST'].CIROOT;

...you can improve the protocol detection.


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

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: 3
(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?


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').

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

[eluser]Colin Williams[/eluser]
I use this, because all the logic you guys are doing is already done by the browser.

$config['base_url'] = '/';

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

$config['base_url'] = '/sub-directory/';

What is wrong with you people?

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

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