Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

localhost/live - detect by HTTP_HOST

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

Is it good idea 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 my code, when setting stuff?

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

This way, I can use the same files live and on localhost, so I can sync without any fear of having wrong e.g. connection settings on live server.

Is it good or wrong idea?

like image 306
Adam Kiss Avatar asked Mar 21 '26 22:03

Adam Kiss


1 Answers

Nothing at all wrong with doing the way you're doing it.

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

Under apache, you could stick something like this:

SetEnv MYAPP_ENVIRONMENT development

in httpd.conf or a suitable .htaccess file

Then in your configuration code:

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

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

or something along those lines.

like image 107
timdev Avatar answered Mar 23 '26 11:03

timdev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!