Yes, but if user call url like: http://www.domain.com/controller/method
will this script return ‘http://www.domain.com’ or ‘http://www.domain.com/controller/method’??
This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.
The active forums are here.
September 05, 2007 11:19am
Subscribe [27]#31 / Apr 23, 2008 11:06pm
Yes, but if user call url like: http://www.domain.com/controller/method
will this script return ‘http://www.domain.com’ or ‘http://www.domain.com/controller/method’??
#32 / Apr 23, 2008 11:21pm
Yes, but if user call url like: http://www.domain.com/controller/method
will this script return ‘http://www.domain.com’ or ‘http://www.domain.com/controller/method’??
only http://www.domain.com/, but if your application is in a subfolder, then http://www.domain.com/subfolder/
#33 / Apr 24, 2008 12:00am
I would just like to say that I use this with CodeExtinguisher and haven’t had problems with myself or anyone using codeextinguisher
#34 / Apr 24, 2008 2:09pm
I neglected to add the last line in the code, though it could probably be pieced together from the previous posts:
$proto = "http" .
((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "s" : "") . "://";
$server = isset($_SERVER['HTTP_HOST']) ?
$_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
$server .= '/';
if (strstr($server, 'localhost'))
$server .= 'subdirectory/';
$config['base_url'] = $proto . $server;As already said, this would return only the base http://www.domain.com/ or http://www.domain.com/subdirectory/ depending on what you specify for “$server .= ‘subdirectory/’ when testing on localhost.
The main advantage we’ve got now is the handling of http/https, you’ll still need to update your subdirectory for each project’s config.php file…
#35 / Apr 24, 2008 4:19pm
I neglected to add the last line in the code, though it could probably be pieced together from the previous posts:
$proto = "http" . ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "s" : "") . "://"; $server = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME']; $server .= '/'; if (strstr($server, 'localhost')) $server .= 'subdirectory/'; $config['base_url'] = $proto . $server;As already said, this would return only the base http://www.domain.com/ or http://www.domain.com/subdirectory/ depending on what you specify for “$server .= ‘subdirectory/’ when testing on localhost.
The main advantage we’ve got now is the handling of http/https, you’ll still need to update your subdirectory for each project’s config.php file…
But my original contribution already handles subdirectory, maybe you can just combine the two, where you handle the protocol.
$proto = "http" .
((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "s" : "") . "://";
$server = isset($_SERVER['HTTP_HOST']) ?
$_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
$server .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);#36 / Apr 25, 2008 1:50am
We’ll need to tweak it a bit more… doesn’t basename() return the final portion of the URL? So with someone accessing a page such as:
http://localhost/subdirectory/controllername/function/parm1/parm2
It would return something like “http://localhost/subdirectory/controllername/function/parm1” with the last piece removed. We could use a simple search for the first / after the root URL to get the subdirectory, if we limit it to one level deep. But what about when it’s launched to http://www.domain.com? Wouldn’t it then interpret the controllername as the subdirectory instead?
Brian
#37 / Apr 25, 2008 3:53am
We’ll need to tweak it a bit more… doesn’t basename() return the final portion of the URL? So with someone accessing a page such as:
http://localhost/subdirectory/controllername/function/parm1/parm2
It would return something like “http://localhost/subdirectory/controllername/function/parm1” with the last piece removed. We could use a simple search for the first / after the root URL to get the subdirectory, if we limit it to one level deep. But what about when it’s launched to http://www.domain.com? Wouldn’t it then interpret the controllername as the subdirectory instead?
Brian
Nope. That’s why we are getting the scriptname, because we are only looking at the location of index.php only, and not actually processing the whole url. Why don’t you try it first so you’ll know.
I’ve tested this approach even if the application is in a sub/sub/subfolder, it wouldn’t matter.
#38 / Apr 25, 2008 11:21pm
I did try it first and it was cutting off the subdirectory, likely since I’m not using index.php (mod rewrite)...
#39 / May 02, 2008 1:26pm
My apologies, I had an extra $server .= “/”; in there from a previous version of the code, your code works just fine!
#40 / May 04, 2008 5:36am
Thanks for this. I will probably change servers a few times so this is always handy 😛
#41 / Aug 08, 2008 3:07pm
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.
#42 / Oct 07, 2008 6:28am
Hi there,
$config[‘base_url’] = “https://”.$_SERVER[‘HTTP_HOST’];
the above works great on intranet, internet and SSL. Thanks! 😊
#43 / Oct 07, 2008 8:08am
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!
#44 / Jul 01, 2009 4:06am
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.
#45 / Sep 25, 2009 2:07pm
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?
TIA