3

我为每个环境使用了适当的配置。为了完成我必须使用“HTTP_HOST”变量来识别,例如:

if($_SERVER['HTTP_HOST']=='example.com'){

问题是当我从 cli 运行代码时,在本地我无法弄清楚环境是什么。

我找到了一些解决我问题的代码

function getIPs($withV6 = true) {
     preg_match_all('/inet'.($withV6 ? '6?' : '').' addr: ?([^ ]+)/', `ifconfig`, $ips);
   return $ips[1];
}
if($_SERVER['HTTP_HOST']=='example.com'||getIPs()[0]=='55.789.258.66'){

ifconfig仅适用于 linux ,我识别哪个操作系统并将 'ifconfig' 更改为 'ipconfig'(windows ) 使用:

$getIpFunc = PHP_OS=='Linux'?`ifconfig`:`ipconfig`;
preg_match_all('/inet'.($withV6 ? '6?' : '').' addr: ?([^ ]+)/', $getIpFunc, $ips);

但我没有在 windows 中收到 ip

4

1 回答 1

1

您可以getHostByName在这两种环境中使用。它可能比使用 ipconfig 或 ifconfig 更可靠。

$localIP = getHostByName(getHostName());

http://php.net/manual/en/function.gethostbyname.php http://php.net/manual/en/function.gethostname.php

于 2017-07-20T10:47:02.387 回答