using args[] to tell which server? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: using args[] to tell which server? (/showthread.php?tid=71695) |
using args[] to tell which server? - richb201 - 09-13-2018 I want to run my app either on the remote server or my localhost. To start it on my localhost I start the Apache and then type localhost in the browser window. To start it on the server I type 54.152.xxx.9/sub_crud on the browser address bar. In my database.cfg I have if ($_SERVER['DOCUMENT_ROOT']="localhost") { $db['default'] = array( 'dsn' => '', 'hostname' => 'localhost', 'username' => 'root', 'password' => 'xxxx', 'database' => 'substantiator', 'dbdriver' => 'mysqli', 'dbprefix' => '', 'pconnect' => FALSE, 'db_debug' => (ENVIRONMENT !== 'production'), 'cache_on' => FALSE, 'cachedir' => '', 'char_set' => 'utf8', 'dbcollat' => 'utf8_general_ci', 'swap_pre' => '', 'encrypt' => FALSE, 'compress' => FALSE, 'stricton' => FALSE, 'failover' => array(), 'save_queries' => TRUE ); } else { $db['default'] = array( 'dsn' => '', 'hostname' => 'localhost', 'username' => 'root', 'password' => 'xxxx', 'database' => 'substantiator', 'dbdriver' => 'mysqli', 'dbprefix' => '', 'pconnect' => FALSE, 'db_debug' => (ENVIRONMENT !== 'production'), 'cache_on' => FALSE, 'cachedir' => '', 'char_set' => 'utf8', 'dbcollat' => 'utf8_general_ci', 'swap_pre' => '', 'encrypt' => FALSE, 'compress' => FALSE, 'stricton' => FALSE, 'failover' => array(), 'save_queries' => TRUE ); I also have this in my config.cfg if ($args[0]="localhost") { $config['base_url'] = 'http://localhost//'; } else { $config['base_url']='http://54.152.xxx.9/sub_crud/'; } For some reason, the localhost database seems to always load. It also seems that DOCUMENT_ROOT is unreliable and seems to be set to localhost even though I am using the 54.152.xxx.9/sub_crud in the browser. Is there a better way to do this? The passwords for the databases on the two servers are different. RE: using args[] to tell which server? - php_rocs - 09-13-2018 @richb201, You could use the application environment feature of CI... (setting environment variable) https://codeigniter.com/user_guide/general/environments.html?highlight=application%20environment#handling-multiple-environments (setting config variables for specific environments) https://codeigniter.com/user_guide/libraries/config.html#environments I use it for development and production environments which are on different servers. RE: using args[] to tell which server? - richb201 - 09-13-2018 That is a pretty cool thing. I realized that I should have used == instead of =, so I will try that first. addendum: I am going to try ENVIRONMENT since argc fails. One question, how do I set the ENVIRONMENT? I created a config/testing directory. In it I have a localhost centric config.php and database.php as the docs say to do. I then modified the index.php like this: define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development'); define ('ENVIRONMENT','testing'); and then I modified a few lines lower to: switch (ENVIRONMENT) { case 'development': case 'testing': error_reporting(-1); // ini_set("error_reporting", E_ALL & ~E_DEPRECATED); //rnb ini_set('display_errors', 1); break; // case 'testing': case 'production': I thought that this would use development, but also use the config/testing config files. But it still uses the regular old config files, instead. RE: using args[] to tell which server? - richb201 - 09-13-2018 Got it working. I used this code in the index.php $_SERVER['CI_ENV']='testing'; //define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development'); define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development'); |