本地主机/活动-通过HTTP_HOST检测 [英] 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?
推荐答案
按照自己的方式做事完全没错.
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.
在Apache下,您可以粘贴以下内容:
Under apache, you could stick something like this:
SetEnv MYAPP_ENVIRONMENT development
在httpd.conf或适当的.htaccess文件中
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';
或类似的内容.
这篇关于本地主机/活动-通过HTTP_HOST检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!