Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter Environment setting

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"
like image 733
Zohaib Avatar asked Nov 15 '13 14:11

Zohaib


People also ask

Where is database set in CodeIgniter?

CodeIgniter has a config file that lets you store your database connection values (username, password, database name, etc.). The config file is located at application/config/database. php. You can also set database connection values for specific environments by placing database.

Where is the config php in CodeIgniter 4?

You will find the application configuration files in the app/Config folder.

What is DSN in CodeIgniter?

dsn. The DSN connect string (an all-in-one configuration sequence). hostname. The hostname of your database server. Often this is 'localhost'.


4 Answers

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
like image 52
bmorenate Avatar answered Oct 18 '22 00:10

bmorenate


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' => ''
    ];
}
like image 7
Rodrigo Queiroz Avatar answered Oct 17 '22 23:10

Rodrigo Queiroz


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;
    }
}
like image 2
Lucky Soni Avatar answered Oct 18 '22 00:10

Lucky Soni


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

if ($_SERVER['HTTP_HOST'] == 'localhost'){
    define('ENVIRONMENT', 'development');
}else{
    define('ENVIRONMENT', 'production');
}
like image 2
Anjani Barnwal Avatar answered Oct 17 '22 23:10

Anjani Barnwal