authenticationFile.txt:
jason:QQAA3322
donald:d5fc4b0e45c8f9a333c0056492c191cf
mickey:bc180dbc583491c00f8a1cd134f7517bNewFile.html:<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="zh" xml:lang="zh" dir="rtl">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Jazz Legends - Miles Davis</title>
</head>
<body>
<form action="index.php" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="user"/></td>
</tr>
<tr>
<td>电子邮件地址:</td>
<td><input type="text" id="email" name="email" size="20" maxlength="40"/></td>
</tr>
<tr>
<td>What's your favorite programming languages?:</td>
<td>
<input type="checkbox" name="languages[]" value="Csharp"/>C#<br />
<input type="checkbox" name="languages[]" value="Java"/>Java<br />
<input type="checkbox" name="languages[]" value="PHP"/>PHP<br />
<input type="checkbox" name="languages[]" value="Perl"/>Perl<br />
<input type="checkbox" name="languages[]" value="HTML"/>HTML<br />
<input type="checkbox" name="languages[]" value="CSS"/>CSS<br />
</td>
</tr>
<tr><td><input type="submit" value="提交" name="submit"/></td></tr>
</table>
</form>
</body>
</html>
index.php:<?php
$authorized = false;
if (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) {
// Read the authentication file into an array
$authFile = file('authenticationFile.txt');
// Search array for authentication match
// If using Windows, use \r\n
if (in_array($_SERVER['PHP_AUTH_USER'].":".$_SERVER['PHP_AUTH_PW'], $authFile)) {
print('OK!<br />');
$authorized = true;
}
}
else {
header('WWW-Authenticate: Basic Realm="Secret Stash"');
header('HTTP/1.1 401 Unauthorized');
print('You must provide the proper credentials!<br />');
$authorized = false;
exit(1);
}
?>
为什么在输入用户名与密码的提示框中填入.txt文件中的其中一项(密码没有采用加密形式的)却不能显示“OK!”,究竟成功与否呢?
jason:QQAA3322
donald:d5fc4b0e45c8f9a333c0056492c191cf
mickey:bc180dbc583491c00f8a1cd134f7517bNewFile.html:<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="zh" xml:lang="zh" dir="rtl">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Jazz Legends - Miles Davis</title>
</head>
<body>
<form action="index.php" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="user"/></td>
</tr>
<tr>
<td>电子邮件地址:</td>
<td><input type="text" id="email" name="email" size="20" maxlength="40"/></td>
</tr>
<tr>
<td>What's your favorite programming languages?:</td>
<td>
<input type="checkbox" name="languages[]" value="Csharp"/>C#<br />
<input type="checkbox" name="languages[]" value="Java"/>Java<br />
<input type="checkbox" name="languages[]" value="PHP"/>PHP<br />
<input type="checkbox" name="languages[]" value="Perl"/>Perl<br />
<input type="checkbox" name="languages[]" value="HTML"/>HTML<br />
<input type="checkbox" name="languages[]" value="CSS"/>CSS<br />
</td>
</tr>
<tr><td><input type="submit" value="提交" name="submit"/></td></tr>
</table>
</form>
</body>
</html>
index.php:<?php
$authorized = false;
if (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) {
// Read the authentication file into an array
$authFile = file('authenticationFile.txt');
// Search array for authentication match
// If using Windows, use \r\n
if (in_array($_SERVER['PHP_AUTH_USER'].":".$_SERVER['PHP_AUTH_PW'], $authFile)) {
print('OK!<br />');
$authorized = true;
}
}
else {
header('WWW-Authenticate: Basic Realm="Secret Stash"');
header('HTTP/1.1 401 Unauthorized');
print('You must provide the proper credentials!<br />');
$authorized = false;
exit(1);
}
?>
为什么在输入用户名与密码的提示框中填入.txt文件中的其中一项(密码没有采用加密形式的)却不能显示“OK!”,究竟成功与否呢?
if ($authFile = file('authenticationFile.txt')) print_r($authFile);
Array ( [0] => jason:QQAA3322 [1] => donald:d5fc4b0e45c8f9a333c0056492c191cf [2] => mickey:bc180dbc583491c00f8a1cd134f7517b )
echo $_SERVER['PHP_AUTH_USER'].":".$_SERVER['PHP_AUTH_PW'];
都没事,输出jason:QQAA3322
看一看是不是我的例子错了,我本想先输入用户名和密码,之后看在.txt文件中有没有相同的一项,有的话就显示出来这样子的。所以我就表单输出和HTTP输出一齐用了而那表单中的用户名和密码文本框我没有删除而已,那是之前一个例子来的,现在这个例子我觉得没有影响就没有删掉。其实一运行直接按提交再在那个HTTP提示框中输入用户名和密码就可以了