Automatic detection of internal / external development environment

We use the following function to automatically detect if we are on a machine inside or on a real server, and then we select the appropriate configurations for the various components:

function devIsLocal(){ $res=false; $http_host=$_SERVER['HTTP_HOST']; if($http_host=='localhost')$res=true; if($http_host=='127.0.0.1')$res=true; if(substr($http_host,-4)=='.lan')$res=true; if(strpos($http_host, '.')===false)$res=true; return($res); } 

As you can see, it depends only on the value of HTTP_HOST.

Of course, if you use some kind of virtual host locally, for example example.com, then the function will be tricked.

Are there other ways to trick a function? and what other variables / places could we look to determine where we are?

+7
php development-environment production-environment autodiscovery
source share
5 answers
 '127.0.0.1' == $_SERVER["REMOTE_ADDR"] 

This will never be evaluated as TRUE in your live system. :)

+10
source share

Set the environment variable in your Apache virtual host configuration. This makes the Zend Framework.

See the ZF Quick Start Guide for an example (Create a Virtual Host section.)

In your httpd.conf or .htaccess file, put:

 SetEnv APPLICATION_ENV "development" 

Then in your application use the getenv function to get the value:

 $environment = getenv("APPLICATION_ENV"); if ($environment == "development") { // do development stuff } else if ($environment == "live") { // do live stuff } 
+15
source share

Adding Andy Schellam to the answer ..

If you use mod_vhost_alias or have different domains with the same (virtual) document root, you can set a variable depending on the parameters, for example.

 SetEnvIf SERVER_ADDR xxxx APPLICATION_ENV=development SetEnvIf HTTP_HOST abc.example.com APPLICATION_ENV=development 
+3
source share

Create and then find the file that exists only on the live server file system.

Of course, your conditions should be as similar as possible; what I suggest looks something like this: in the directory / var / environment / there is a file called {devel | test | qa | staging | live}, depending on the server you are on, then just check the file name.

Of course, you need to exclude this file from version control and from any build process you may have.

+1
source share

Of course, if you use a virtual host locally, for example example.com, then the function will be tricked.

Also, if the host is not local, but uses widlcard or default vhost defn, and the user adds the IP address to the hosts file locally.

I would recommend having a directory on the include path, which also exists in real time, but does not replicate there - and just saves:

 function getEnv(){ return 'Live'; } 

or

 function getEnv(){ return 'Test'; } 

If both envs are on the same server, you can still do this by setting the include_path to the Apache or .htaccess configuration.

It also allows you to separate potentially sensitive specific env data - such as database hosts / passwords and passkeys.

FROM.

0
source share

All Articles