$_SERVER['SERVER_NAME'] 的字符串长度不正确
Incorrect string length of $_SERVER['SERVER_NAME']
所以我试图检查 Apache 服务器名称是否包含特定字符串,并注意到非常奇怪的行为(例如,即使字符串看起来完全匹配,if 语句中也没有匹配项)。使用 var_dump()
我查看了包含服务器名称的变量,令我惊讶的是我看到了这个:
string(11) "test.local:5757"
如果不计算数字,字符串只有11个字符。如果我使用 'test.local:5757'
而不是 $_SERVER['SERVER_NAME']
声明变量,我得到正确的长度 15。
我试过在 "reset" 字符串的末尾附加一个空字符串,我什至尝试在字符串上添加额外的字母,最终被计算在内,但 5757 仍然没有被计算在内.
有没有人有过这样的经历??
编辑:抱歉,发帖后我没有包含足够的信息。
我遗漏的一个主要细节是我在 MAMP 之上使用 CodeKit。我的本地 MAMP 安装位于 localhost
,我的 CodeKit 项目的 URL 是 test.local:5757'
。但是,当我回显声明为 $_SERVER['SERVER_NAME']
的变量时,似乎确实显示了 5757 端口。更奇怪的是,回显 $_SERVER['SERVER_NAME'] . ':' $_SERVER['SERVER_PORT']
打印 test.local:5757
,同时将冒号更改为任何其他字符,例如 $_SERVER['SERVER_NAME'] . '>' $_SERVER['SERVER_PORT']
,打印 test.local:5757>80
。
以下是我所看到的更多示例:
$host = $_SERVER['SERVER_NAME'];
echo $host;
// prints 'test.local:5757'
$host = $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'];
echo $host;
// prints 'test.local:5757'
$host = $_SERVER['SERVER_NAME'] . '&' . $_SERVER['SERVER_PORT'];
echo $host;
// prints 'test.local:5757&80'
$host = $_SERVER['SERVER_NAME'];
echo $host;
// prints 'test.local:5757'
$bool = false;
if ($host == 'test.local:5757') $bool = true;
echo $bool;
// prints false;
$host = $_SERVER['SERVER_NAME'];
var dump($host);
// prints string(11) test.local:5757
抱歉没有标记为 CodeKit 相关。非常感谢任何帮助!
这里有一个很好的解释 https://github.com/bdkjones/CodeKit/issues/440 以
结尾
In short, there's nothing wrong here --> MAMP has the correct Host header.
由于您试图区分 test.local 和本地主机,您可以使用:
if (strpos($_SERVER['HOST_NAME'],'localhost') !== false) {
echo 'localhost';
} else {
echo 'not localhost';
}
所以我试图检查 Apache 服务器名称是否包含特定字符串,并注意到非常奇怪的行为(例如,即使字符串看起来完全匹配,if 语句中也没有匹配项)。使用 var_dump()
我查看了包含服务器名称的变量,令我惊讶的是我看到了这个:
string(11) "test.local:5757"
如果不计算数字,字符串只有11个字符。如果我使用 'test.local:5757'
而不是 $_SERVER['SERVER_NAME']
声明变量,我得到正确的长度 15。
我试过在 "reset" 字符串的末尾附加一个空字符串,我什至尝试在字符串上添加额外的字母,最终被计算在内,但 5757 仍然没有被计算在内.
有没有人有过这样的经历??
编辑:抱歉,发帖后我没有包含足够的信息。
我遗漏的一个主要细节是我在 MAMP 之上使用 CodeKit。我的本地 MAMP 安装位于 localhost
,我的 CodeKit 项目的 URL 是 test.local:5757'
。但是,当我回显声明为 $_SERVER['SERVER_NAME']
的变量时,似乎确实显示了 5757 端口。更奇怪的是,回显 $_SERVER['SERVER_NAME'] . ':' $_SERVER['SERVER_PORT']
打印 test.local:5757
,同时将冒号更改为任何其他字符,例如 $_SERVER['SERVER_NAME'] . '>' $_SERVER['SERVER_PORT']
,打印 test.local:5757>80
。
以下是我所看到的更多示例:
$host = $_SERVER['SERVER_NAME'];
echo $host;
// prints 'test.local:5757'
$host = $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'];
echo $host;
// prints 'test.local:5757'
$host = $_SERVER['SERVER_NAME'] . '&' . $_SERVER['SERVER_PORT'];
echo $host;
// prints 'test.local:5757&80'
$host = $_SERVER['SERVER_NAME'];
echo $host;
// prints 'test.local:5757'
$bool = false;
if ($host == 'test.local:5757') $bool = true;
echo $bool;
// prints false;
$host = $_SERVER['SERVER_NAME'];
var dump($host);
// prints string(11) test.local:5757
抱歉没有标记为 CodeKit 相关。非常感谢任何帮助!
这里有一个很好的解释 https://github.com/bdkjones/CodeKit/issues/440 以
结尾In short, there's nothing wrong here --> MAMP has the correct Host header.
由于您试图区分 test.local 和本地主机,您可以使用:
if (strpos($_SERVER['HOST_NAME'],'localhost') !== false) {
echo 'localhost';
} else {
echo 'not localhost';
}