I have a different config files for a script that runs both on a dev and production server. The question is, can I detect somehow which is which ?
- You'd go more relevant answers if the question could bring some details (what did you tried so far, type of the config files, limitations of your CLI etc.)takeshin– takeshin2011-07-22 17:21:13 +00:00Commented Jul 22, 2011 at 17:21
- Well, I would need a good solution for a reseller account where you only have .htaccess and php.ini at your disposal. Why I need is a good practice to make the difference between the two environment.johnlemon– johnlemon2011-07-22 18:13:42 +00:00Commented Jul 22, 2011 at 18:13
- Thanks for pointing that out. See the edit to my answer.takeshin– takeshin2011-07-22 18:32:42 +00:00Commented Jul 22, 2011 at 18:32
3 Answers
Have a different bootstrap file to include depending on the server you're on. So
include "bootstrap.php"; // your script etc.. On the production box, bootstrap.php will have a constant IS_DEV = FALSE On the development box, bootstrap.php will have the same constant IS_DEV = TRUE
So the same script will include the same file name, bootstrap.php, but the contents of bootstrap will have different values for the IS_DEV constant variable.
2 Comments
$_SERVER variables are not available when the script is run in CLI, so this won't work.Some solutions to choose from:
- use
config.iniandconfig.ini.diststrategy (.distread when the other is not present) - set some environment variable in
.htaccessor virtual host config and read it usinggetenv - check for existence of some file which does not exist on the other machine
- compare the current script path
- determine server IP
Basically, you just need to compare anything that differs on those two machines, or set some variable as a reference.
Edit after comment to the question:
Example with .htaccess:
In .htaccess (only on the staging server):
SetEnv APPLICATION_ENVIRONMENT staging In your PHP script:
if ('staging' === getenv('APPLICATION_ENVIRONMENT')) { // use config_staging.ini } else { // use config_develop.ini } Note you may have everything in one config file, just divide it to sections.
I think this may be called a good practice, as it is recommended in Zend Framework manual.
If you have access to the console (you have for sure in the development environment), you may set this flag via shell variable:
export APPLICATION_ENVIRONMENT=development on Windows:
SET APPLICATION_ENVIRONMENT=development and then read it using getenv from PHP.
1 Comment
if (PHP_SAPI === 'cli') { echo 'running in console!'; }