【发布时间】:2015-02-15 17:21:33
【问题描述】:
在 Welling 和 Thomson 的书中编写以下代码
if (($_SERVER['PHP_AUTH_USER'] != 'user') ||
($_SERVER['PHP_AUTH_PW'] != 'pass')) {
// visitor has not yet given details, or their
// name and password combination are not correct
header('WWW-Authenticate: Basic realm="Realm-Name"');
header('HTTP/1.0 401 Unauthorized');
echo "<h1>Go Away!</h1>
<p>You are not authorized to view this resource.</p>";
} else {
// visitor has provided correct details
echo "<h1>Here it is!</h1>
<p>I bet you are glad you can see this secret page.</p>";
}
header('HTTP/1.0 401 Unauthorized') 是做什么的?我删除了这一行,脚本正常工作。
为什么这段代码没有这一行也能工作?
【问题讨论】:
-
应该设置状态码。对于大多数设置,使用
HTTP/1.0磁头是不正确的。通常应该是Status:。 -- 然而,您认为移除它后一切“正常工作”的评估似乎不太可能。 -
这段代码在我的服务器上似乎可以正常运行....您认为没有正常运行的原因是什么?
-
@Cragmonkey 我想知道为什么这段代码没有这条线可以工作?感谢您的关注。
标签: php http-headers basic-authentication unauthorized