自动检测内部/外部开发环境

发布于 2024-08-28 18:53:09 字数 502 浏览 1 评论 0原文

我们使用以下函数来自动检测我们是在内部计算机上还是在实时服务器上,然后为各种组件选择适当的配置:

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);

}

如您所见,它仅依赖于 HTTP_HOST 值。

当然,如果您在本地使用某种虚拟主机(例如 example.com),那么该功能就会被欺骗。

还有其他方法可以欺骗该函数吗?我们还可以查看哪些其他变量/位置来确定我们所处的位置?

We use the following function to auto detect if we are on a machine internally or on a live server and then choose the appropriate configs for 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 only relies on the HTTP_HOST value.

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

Are there any other ways to fool the function? and what other variables/places could we peek at to determine where we are?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

铁憨憨 2024-09-04 18:53:09

在 Apache 虚拟主机配置中设置环境变量。 Zend 框架就是这样做的。

有关示例,请参阅 ZF 快速入门指南( “创建虚拟主机”部分。)

在您的 httpd.conf 或 .htaccess 文件中,输入:

SetEnv APPLICATION_ENV "development"

然后在您的应用程序中,使用 getenv 函数来获取值:

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

Set an environment variable in your Apache virtual host configuration. This is the way the Zend Framework does it.

See the ZF quick start guide for an example (the "Create a Virtual Host" section.)

In your httpd.conf or a .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
}
我恋#小黄人 2024-09-04 18:53:09
'127.0.0.1' == $_SERVER["REMOTE_ADDR"]

在您的实时系统上,这永远不会评估为 TRUE。 :)

'127.0.0.1' == $_SERVER["REMOTE_ADDR"]

This will never evaluate as TRUE on your live system. :)

巡山小妖精 2024-09-04 18:53:09

添加到 Andy Shellam 的答案..

如果您使用的是 mod_vhost_alias ,或者具有具有相同(虚拟)文档根的多个域,您可以根据参数设置变量,例如

SetEnvIf SERVER_ADDR x.x.x.x APPLICATION_ENV=development
SetEnvIf HTTP_HOST abc.example.com APPLICATION_ENV=development

Adding to Andy Shellam's answer..

If you are using mod_vhost_alias, or have various domains with the same (virtual) document root, you can set the variable dependent upon parameters, e.g.

SetEnvIf SERVER_ADDR x.x.x.x APPLICATION_ENV=development
SetEnvIf HTTP_HOST abc.example.com APPLICATION_ENV=development
此岸叶落 2024-09-04 18:53:09

创建并稍后查找仅存在于实时服务器文件系统上的文件。

当然,您的环境应该尽可能相似;我的建议是这样的:在目录 /var/environment/ 中,有一个名为 {devel|test|qa|staging|live} 的文件,具体取决于您所在的服务器 - 然后只需检查文件名。

当然,您需要将此文件从版本控制和您可能拥有的任何构建过程中排除。

Create and later look for a file that only exists on the live server's filesystem.

Granted, your environments should be as similar as possible; what I'm suggesting is something like this: in directory /var/environment/, have a file named {devel|test|qa|staging|live}, depending on the server you're on - then just check the filename.

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

岁月静好 2024-09-04 18:53:09

当然,如果你在本地使用了虚拟主机(例如 example.com),那么该功能就会被欺骗。

此外,如果主机不是本地的,但使用widlcard或默认虚拟主机定义,并且用户将IP地址添加到本地的hosts文件中。

我建议在包含路径上有一个目录,该目录也存在于实时环境中,但不会复制到那里 - 并且简单地存储:

function getEnv(){
  return 'Live';
}

或者

function getEnv(){
  return 'Test';
}

如果两个环境都在同一服务器上 - 您仍然可以通过在 Apache config 或 .htaccess 中设置 include_path 来做到这一点。

这还允许您分离潜在敏感的环境特定数据 - 例如数据库主机/密码和加密密钥。

C.

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

Also if the host is not local but uses a 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 on live but is not replicated there - and simply store:

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 in Apache config or .htaccess.

This also allows you to seperate potentially sensitive env specific data - like database hosts/passwords and encyption keys.

C.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文