13

Codeigniter development environment is not setting. I always use this code in index.php. but i don't understand why i am getting "production" as output while i am working on localhost.

switch(dirname(__FILE__)){
 case "H:\wamp\www\sitedirectory":
    define('ENVIRONMENT', 'development');
 break;
 default:
    define('ENVIRONMENT', 'production');
 break;
 }

  echo ENVIRONMENT ;   // output is "production" while i am on localhost
  echo dirname(__FILE__) ;  // output is "H:\wamp\www\sitedirectory"
Zohaib
  • 328
  • 1
  • 3
  • 15

5 Answers5

30

That's strange. It did the exact same thing for me. Could you try something like this?

switch($_SERVER["HTTP_HOST"]){
 case "localhost":
    define('ENVIRONMENT', 'development');
 break;
 default:
    define('ENVIRONMENT', 'production');
 break;
 }

 echo ENVIRONMENT ;   // output development
bmorenate
  • 963
  • 1
  • 8
  • 18
  • Thanks it works. dirname(__FILE__) method works for me always but i dont understand why this is creating problem in this project. Thanks again – Zohaib Nov 15 '13 at 14:26
7

To dynamically set the ENVIRONMENT based on the server's IP, below I have used regular expression to check for local IP's such as 127.0.* and 10.0.*.

At the root of you project look to index.php and replace:

define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development');

with:

$server_ip = getHostByName(getHostName());

if (preg_match("/^(127\.0\.|10\.0\.).+/i", $server_ip)) {
    define("ENVIRONMENT", "development");
    define("BASEURL", "http://localhost:8000/");
} else {
    define("ENVIRONMENT", "production");
    define("BASEURL", "https://domain.com/");
}

Make sure to replace the value from BASEURL with your own and in application/config/config.php add:

$config['base_url'] = BASEURL;

To improve further add to application/config/database.php right before the database settings $db['default'] = array(:

if(ENVIRONMENT !== 'production') {
    $db = [
            'username' => '',
            'password' => '',
            'database' => '',
            'hostname' => '127.0.0.1'
    ];
} else {
    $db = [
            'username' => '',
            'password' => '',
            'database' => '',
            'hostname' => ''
    ];
}
Rodrigo Queiroz
  • 2,674
  • 24
  • 30
2

Adding on to the other answers. Now the below answer might look like an overkill (if you have to define the environment variables then why use the HTTP_HOST at all? Well in my experience CI fails to reflect any changes made to the environment variables even after restarting apache. It somehow gets the updated values when sending a request from the CLI.)

if (php_sapi_name() === 'cli') 
{
    // incase the request is made using the cli, the $_SERVER['HTTP_HOST'] will not be set

    define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development');
} 
else 
{
    switch ($_SERVER["HTTP_HOST"]) 
    {
        case "localhost":
            define('ENVIRONMENT', 'development');
            break;
        default:
            define('ENVIRONMENT', 'production');
            break;
    }
}
Lucky Soni
  • 6,811
  • 3
  • 38
  • 57
2

You can Also use this one to auto change Environment in codeigniter

if ($_SERVER['HTTP_HOST'] == 'localhost'){
    define('ENVIRONMENT', 'development');
}else{
    define('ENVIRONMENT', 'production');
}
Anjani Barnwal
  • 1,362
  • 1
  • 17
  • 23
0

Set you ENVIRONMENT before the following line in the index.php file.(CI 4.0.4)

$app = require rtrim($paths->systemDirectory, '/ ') . '/bootstrap.php';

At some point the ENVIRONMENT is set and you can no longer change it with the define.

Macfredd
  • 1
  • 1