views:

99

answers:

3

I'm running Windows 7. Within, I'm running Virtualbox and Ubuntu. Within Ubuntu, I'm running Apache and using PHP codeigniter. To reference our CSS and JS files, we use this syntax:

<?php echo site_url('css/styles.css'); ?>

When viewing the site locally (within Ubuntu VM) that resolves to:

http://localhost/styles.css

So works fine.

The catch is when I access that site from within Windows 7 (for IE testing). To access the site in Windows 7, I refer to the IP address of the VM:

http://xx.x.x.x/

That loads the site but, alas, all the site_url variables are still being rewritten as 'localhost'. Said files obviously don't exist on the localhost of my windows 7.

Is there a way around this short of just hard-coding relative links in my PHP file?

+1  A: 

The site_url() function in CodeIgniter, documented here, uses whatever you have set up in your configuration file. You should just be able to open your main config file (application/config/config.php) and change the base URL setting to your VM IP, and then your links will work for both environments. It's the first setting in the file:

$config['base_url'] = "http://x.x.x.x/";
zombat
ah! I didn't realize that was just a variable set in the config file. Good to know!
DA
A: 

More practical would be to set up a virtual host on Apache and adding a host name to the windows 'hosts' file, so that you can use something like 'mysite.local' instead of 'localhost' or ip address.

more info

stereofrog
+1  A: 

Many options here from very simple to crazy complex:

Easy

$config['base_url'] = 'http://'. $_SERVER['HTTP_HOST'].'/';

This supports:

Complex

$config['base_url'] = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ? 'https' : 'http';
$config['base_url'] .= '://'. $_SERVER['HTTP_HOST'];
$config['base_url']l .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);

This supports:

Potentially insane

This one is very similar but keeps your config.php clean, offers a fallback if HTTP_HOST fails, gives you a constant to use throughout the site and can give you a relative URI to BASEPATH and APPPATH from web root too.

Put this in constants.php:

/*
|--------------------------------------------------------------------------
| Docment root folders
|--------------------------------------------------------------------------
|
| These constants use existing location information to work out web root, etc.
|
*/

// Base URL (keeps this crazy sh*t out of the config.php
if(isset($_SERVER['HTTP_HOST']))
{
    $base_url = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ? 'https' : 'http';
    $base_url .= '://'. $_SERVER['HTTP_HOST'];
    $base_url .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);

    // Base URI (It's different to base URL!)
    $base_uri = parse_url($base_url, PHP_URL_PATH);
    if(substr($base_uri, 0, 1) != '/') $base_uri = '/'.$base_uri;
    if(substr($base_uri, -1, 1) != '/') $base_uri .= '/';
}

else
{
    $base_url = 'http://localhost/';
    $base_uri = '/';
}

// Define these values to be used later on
define('BASE_URL', $base_url);
define('BASE_URI', $base_uri);
define('APPPATH_URI', BASE_URI.APPPATH);

// We dont need these variables any more
unset($base_uri, $base_url);

In your config.php you can simply use:

$config['base_url'] = BASE_URL;

This supports:

(Yes, that's the same list as the complex method).

This should get your app running pretty much anywhere you put it, but as of yet it does not support /~userdir URL's.

Phil Sturgeon