CodeIgniter Forums
base_url() wrong even when explicitly set - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: base_url() wrong even when explicitly set (/showthread.php?tid=51722)



base_url() wrong even when explicitly set - El Forum - 05-15-2012

[eluser]madshadow[/eluser]
I have multiple environments set up for my application (development, staging, demo, production). Everything was working smoothly among the environments until I made some kind of change to enact more dynamic environment setting. The strange thing is that it works fine on my localhost and demo server, but not on staging when I am at staging.domain.com.

To debug, my main landing page echoes out the ENVIRONMENT variable (which correctly shows 'staging') and then I echo out base_url which incorrectly shows (http://localhost/domain). For some reason this is only happening on staging and not on demo or localhost. I even tried removing the dynamic base_url generation and hard-coded "staging.domain.com" but it is still showing as "localhost/domain" - I am sure this is something stupid I am missing but I have been beating my head over this. Does anyone see what I'm missing? Any help would be greatly appreciated. Thank you in advance

I have the following in my index.php file to set the environment variable:

Code:
if (isset($_SERVER['PLATFORM']))
{
    switch ($_SERVER['PLATFORM']) {

        case 'PAGODA':
            define('ENVIRONMENT', 'demo');
        break;
    // add additional cases for more environments
    }
}
else
{
    if (strpos($_SERVER['SERVER_NAME'], 'local') !== FALSE)
{
   define('ENVIRONMENT', 'development');
}
elseif (strpos($_SERVER['SERVER_NAME'], 'staging.') === 0)
{
   define('ENVIRONMENT', 'staging');
}
elseif (strpos($_SERVER['SERVER_NAME'], 'demo.') === 0)
{
   define('ENVIRONMENT', 'demo');
}
else
{
   define('ENVIRONMENT', 'production');
}
}

I have the following to set the url in my config.php file (actual domain name redacted):

Code:
if (defined('ENVIRONMENT'))
{
switch (ENVIRONMENT)
{
  case 'development':
   $config['base_url'] = "http://localhost/domain/";
  break;

  case 'staging':
   $config['base_url'] = "http://staging.domain.com";
  break;
  
  case 'production':
   $config['base_url'] = "http://www.domain.com/";
  break;

  case 'demo':
   $config['base_url'] = "http://demo.domain.com/";
  break;

  default:
   $config['base_url'] = "http://www.domain.com/";
  
}
}
else
{
$config['base_url'] = "http://www.domain.com/";

}

Finally, if it matters, I have the following in .htaccess file:

Code:
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]



base_url() wrong even when explicitly set - El Forum - 05-15-2012

[eluser]gRoberts[/eluser]
try var_dump'ing the $_SERVER variable and see what it says in there?

Code:
<? var_dump($_SERVER); ?>



base_url() wrong even when explicitly set - El Forum - 05-15-2012

[eluser]madshadow[/eluser]
The $_SERVER['SERVER_NAME'] variable properly shows staging.domain.com . This is maddening!


base_url() wrong even when explicitly set - El Forum - 05-15-2012

[eluser]Aken[/eluser]
Your strpos() checks in your index.php are wrong. You're checking for === 0 instead of !== false. That's why only your development mode works properly.


base_url() wrong even when explicitly set - El Forum - 05-15-2012

[eluser]madshadow[/eluser]
[quote author="Aken" date="1337108007"]Your strpos() checks in your index.php are wrong. You're checking for === 0 instead of !== false. That's why only your development mode works properly.[/quote]

Aken- Thanks for replying. As I mention above, though, 1) the demo environment is working properly and 2) even in the staging environment the ENVIRONMENT variable is correctly set to "staging." The disconnect seems to be in the base_url being set based on that staging ENVIRONMENT variable.


base_url() wrong even when explicitly set - El Forum - 05-15-2012

[eluser]Aken[/eluser]
print_r() the $config array in the config file to see what it is set to at that point. If it's correct, then it is your own code changing it somewhere else.