5

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 ?

3
  • 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.) Commented 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. Commented Jul 22, 2011 at 18:13
  • Thanks for pointing that out. See the edit to my answer. Commented Jul 22, 2011 at 18:32

3 Answers 3

6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

$_SERVER variables are not available when the script is run in CLI, so this won't work.
@takeshin k, then I removed that answer. Using a bootstrap file will also be effective.
2

Some solutions to choose from:

  • use config.ini and config.ini.dist strategy (.dist read when the other is not present)
  • set some environment variable in .htaccess or virtual host config and read it using getenv
  • 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

Sorry for the confusion: even If i have access to the .htaccess file the problem I'm looking to solve is for a script running in CLI. So using .htaccess to solve this the usual way won't work.
-1
if (PHP_SAPI === 'cli') { echo 'running in console!'; } 

1 Comment

This distinguishes between the different SAPIs, but not where there are running.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.