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"
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.
You will find the application configuration files in the app/Config folder.
dsn. The DSN connect string (an all-in-one configuration sequence). hostname. The hostname of your database server. Often this is 'localhost'.
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
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' => ''
];
}
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;
}
}
You can Also use this one to auto change Environment in codeigniter
if ($_SERVER['HTTP_HOST'] == 'localhost'){
define('ENVIRONMENT', 'development');
}else{
define('ENVIRONMENT', 'production');
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With