5

I have a public facing debug script that I would only like to run on certain dev boxes, I was hoping to do this programatically inside this script, by detecting the server ip or name-

So I have a question about the security of $_SERVER and $_SERVER['HTTP_HOST'] in particular.

From this: http://shiflett.org/blog/2006/mar/server-name-versus-http-host blog post I have gathered that this var is pretty insecure, and can't be trusted.

What is the best way to find out from php what box you are currently on?

I thought of using FILE , since that seems to be pretty secure, but I'm not sure I have enough info just from the file path.

I don't necessarily need the server name, even ip would be fine.

thanks in advance.

4

4 回答 4

3

最好的方法?这取决于您对环境的控制程度。以下是一些选项:

  1. 通过网络服务器设置环境变量以指示该框。

    if (getenv('env_server') == 'production')
    

    这很好,因为没有您需要担心的文件。只是网络服务器配置。

  2. 在服务器上的“已知”位置设置一个文件,并检查该文件(整个服务器的一个文件)。

    require('/path/to/environment.php');
    

    该文件应该定义一个常量来确定环境。

  3. 为服务器手动配置每个应用程序。这是最容易做到的,因为它不需要服务器端的任何东西,但它也是最不方便的,因为您需要手动配置每个安装......

  4. 用于访问站点的外部 IP 地址:

    $_SERVER['SERVER_ADDR']

    这很好,因为它不需要在服务器端进行额外配置。但这将要求您保留所有活动 IP 地址的映射,以及它们绑定到的服务器(特别是因为超过 1 个 IP 可以指向同一服务器)...

于 2011-02-17T02:17:41.940 回答
2

最好的方法是通过在机器上放置环境配置文件并检查它来明确定义机器:

if (file_exists('environment.php')) {
    include 'environment.php';
}

此文件可能仅包含您所在机器的名称,或者$debug = 0您想要为特定机器自定义的配置设置或其他任何内容。

于 2011-02-17T01:57:58.837 回答
0

从 php 中找出您目前在哪个盒子上的最佳方法是什么?

$_SERVER["SERVER_NAME"]通常会没事的。克里斯概述的安全问题需要非常非常具体的环境才能发挥作用。

$_SERVER["SERVER_ADDR"]也将是一个替代方案。

除此之外,__FILE__如果有机会从路径中获得提示,我会倾向于选择。

于 2011-02-17T01:56:13.893 回答
0

您可以使用exec()来运行 shell 命令。

echo exec('hostname');

这可以获取 OS X 上的 IP 地址,可能是特定于平台的。

echo exec('ifconfig | grep "inet " | grep -v 127.0.0.1 | cut -d\  -f2');
于 2011-02-17T01:59:04.607 回答