Localhost / live - discovery by HTTP_HOST

let's say I develop locally and debug small things on a real server.

Is it good to have something like this in my code?

$is_local = (strpos($_SERVER['HTTP_HOST'], 'localhost') !== false);
define ('DEBUG',$is_local);

And then use it through your code when setting up?

$mysql_settings = (DEBUG) ? 
  array(/*localhost settings*/) : 
  array(/*live settings*/);

Thus, I can use the same files live and on the local host, so I can synchronize without fear of error, for example. connection settings on a real server.

Is this a good or wrong idea?

+3
source share
3 answers

Nothing wrong with the way you do it.

Another strategy is to set some environment variable for your development (or other, non-production) system.

Under apache, you can insert something like this:

SetEnv MYAPP_ENVIRONMENT development

httpd.conf .htaccess

:

if (! getenv('MYAPP_ENVIRONMENT')){
  $env = 'production';
}else{
  $env = getenv('MYAPP_ENVIRONMENT"));
}

require_once 'conf/config.' . $env . '.php';

- .

+5

, , , .

$is_local:

$is_local = (strpos($_SERVER['http_host'], 'localhost') !== false);

TRUE localhostIsAwesome.com.

, , , .

- $_SERVER[ 'SERVER_NAME' ]

+2

,

  • : .

,

  • , dev, test, production

, , , conf.dev.php, conf.test.php, conf.prod.php , env,

$env = 'production';

, env, $env, :

require_once 'conf.'.$env.'.php';

conf.dev.php git/hg/svn ignore, , conf.production.php .

.

+1

Source: https://habr.com/ru/post/1743696/


All Articles