gpt4 book ai didi

php - 如何在 PHP 中使用基本 HTTP 身份验证?

转载 作者:IT王子 更新时间:2023-10-29 00:22:06 25 4
gpt4 key购买 nike

我正在尝试使用 Basic HTTP Authentication并遵循 PHP 手册页上的示例。但这对我不起作用。似乎未设置变量 $_SERVER['PHP_AUTH_USER']。当用户尝试登录时,系统会提示用户使用新的登录对话框。服务器运行的是 PHP 5.3.3,我已尝试使用 Google Chrome 和 Internet Explorer。

这是我使用的简单示例:

<?php
if (!isset($_SERVER['PHP_AUTH_USER'])) {
header('WWW-Authenticate: Basic realm="Jonas Realm"');
header('HTTP/1.0 401 Unauthorized');
echo 'User pressed Cancel';
exit;
} else {
echo "<p>Hello {$_SERVER['PHP_AUTH_USER']}.</p>";
echo "<p>You entered {$_SERVER['PHP_AUTH_PW']} as you password.</p>";
}
?>

怎么了?如何在 PHP 中使用基本 HTTP 身份验证?

最佳答案

试试这个:

<?php
/*
** Define a couple of functions for
** starting and ending an HTML document
*/
function startPage()
{
print("<html>\n");
print("<head>\n");
print("<title>Listing 24-1</title>\n");
print("</head>\n");
print("<body>\n");
}

function endPage()
{
print("</body>\n");
print("</html>\n");
}
/*
** test for username/password
*/
if( ( isset($_SERVER['PHP_AUTH_USER'] ) && ( $_SERVER['PHP_AUTH_USER'] == "leon" ) ) AND
( isset($_SERVER['PHP_AUTH_PW'] ) && ( $_SERVER['PHP_AUTH_PW'] == "secret" )) )
{
startPage();

print("You have logged in successfully!<br>\n");

endPage();
}
else
{
//Send headers to cause a browser to request
//username and password from user
header("WWW-Authenticate: " .
"Basic realm=\"Leon's Protected Area\"");
header("HTTP/1.0 401 Unauthorized");

//Show failure text, which browsers usually
//show only after several failed attempts
print("This page is protected by HTTP " .
"Authentication.<br>\nUse <b>leon</b> " .
"for the username, and <b>secret</b> " .
"for the password.<br>\n");
}
?>

关于php - 如何在 PHP 中使用基本 HTTP 身份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4150507/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com