1

I have 2 servers for my PHP project, one for prod and one for tests.

I want to add to the test server a flag to use it in every PHP file. For example to add define('TEST', true)

How can I put this define in the "server level"? I mean that it will be define in the ini or somthing, not in PHP page.

nrofis
  • 8,975
  • 14
  • 58
  • 113
  • 1
    You can try using environment variables instead.. http://stackoverflow.com/questions/17550223/set-an-environment-variable-in-htaccess-and-retrieve-it-in-php – Jeff Lambert Jun 19 '14 at 16:53
  • Duplicate? http://stackoverflow.com/questions/11427681/how-to-define-a-php-config-file-that-define-constant-and-use-it-any-where-in-pro – bloodyKnuckles Jun 19 '14 at 16:54
  • They are in the `.htaccess` file. I don't want to set this flag in any file that can be upload later to the prod server. – nrofis Jun 19 '14 at 16:55
  • Why an env var? You could simply check the name of the server, e.g. hostname, `$_SERVER['HTTP_HOST']`, etc... There will be SOME different between your prod and devel servers that you can check for without explicitly setting a flag for it. – Marc B Jun 19 '14 at 16:56
  • @bloodyKnuckles No, my main problem is where can I define it. – nrofis Jun 19 '14 at 16:56
  • @nrofis You should define it in your server config (e.g. [Apache SetEnv](http://httpd.apache.org/docs/2.2/mod/mod_env.html#setenv)). – cmbuckley Jun 19 '14 at 16:56
  • @cbuckley How? I didn't find example for this. My servers are Apache 2.4 – nrofis Jun 19 '14 at 16:58
  • The link is above, or [here for 2.4](http://httpd.apache.org/docs/2.4/mod/mod_env.html). – cmbuckley Jun 19 '14 at 16:59

2 Answers2

2

You should use environment variables for this.

In Apache, for instance, you can use mod_env to set an environment variable in your VirtualHost directive for your test server:  

SetEnv APPLICATION_ENV testing

And for your live server:

SetEnv APPLICATION_ENV production

You would then use it in PHP:

$environment = getenv('APPLICATION_ENV');

if ($environment == 'testing') {
    // ...
}
nrofis
  • 8,975
  • 14
  • 58
  • 113
cmbuckley
  • 40,217
  • 9
  • 77
  • 91
  • It can be written in every place, not just in the `VirtualHost` – nrofis Jun 19 '14 at 17:09
  • The [Context of SetEnv](http://httpd.apache.org/docs/current/mod/mod_env.html#setenv) is "server config, virtual host, directory, .htaccess". The lines above are directly from my own `VirtualHost` blocks :-) – cmbuckley Jun 20 '14 at 07:39
-1

Personally, I like to use $myname = `hostname`;

Then if the hostname is the name of my test server, put it in test mode.

Alternatives exist, such as checking the HTTP_HOST if you're working on a subdomain. Or you could find out the port being connected on if you use the same domain but another port.

Basically, anything that makes them different, can be used as a check to put you in test mode.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592