练习PHP登录代码是无限循环的
我刚刚从一本名叫HeadFirst PHP和MySQL的书中学习了PHP和MySQL的基础知识,在其中一个教程中遇到了一个障碍。这是一个基本的表单,可以输入预设的用户名和密码并访问页面。然而,不管我是正确还是错误地登录到,它只是将我循环回登录表单。在谷歌Chrome上,这是一个完全的循环,在Safari上,它说我输入了错误的信息,尽管输入了正确的信息。我还没有找到任何答案 下面是authorize.php代码练习PHP登录代码是无限循环的,php,mysql,loops,login,Php,Mysql,Loops,Login,我刚刚从一本名叫HeadFirst PHP和MySQL的书中学习了PHP和MySQL的基础知识,在其中一个教程中遇到了一个障碍。这是一个基本的表单,可以输入预设的用户名和密码并访问页面。然而,不管我是正确还是错误地登录到,它只是将我循环回登录表单。在谷歌Chrome上,这是一个完全的循环,在Safari上,它说我输入了错误的信息,尽管输入了正确的信息。我还没有找到任何答案 下面是authorize.php代码 <?php // User name and password fo
<?php
// User name and password for authentication
$username = 'rock';
$password = 'roll';
if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']) ||
($_SERVER['PHP_AUTH_USER'] != $username) || ($_SERVER['PHP_AUTH_PW'] != $password)) {
// The user name/password are incorrect so send the authentication headers
header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: Basic realm="Guitar Wars"');
exit('<h2>Guitar Wars</h2>Sorry, you must enter a valid user name and password to access this page.');
}
?>
您可以在这里查看教程。。。“它只是让我回到登录表单。”-什么表单?@Fred ii-他在使用HTTP基本身份验证。该表单通过本机浏览器提示符提供@Maxantoucci我看不出代码有任何明显的错误。您可以在您的if
语句之前尝试使用var\u dump($\u SERVER)
,以确保PHP正确设置PHP\u AUTH\u USER
和PHP\u AUTH\u PW
字段。@Max Same在这里,代码也为我签出@好的,我明白了为什么OP贴上了“表单”而不是“弹出对话框”这个词。我看不到我的评论和实际问题之间有直接的联系,但是。。。authorize.php真的是以开头的<?php
标记前的空格开头,还是仅仅是文章中的一种类型?如果是,它会在实际调用头()之前生成输出,实际情况不应该是这样:。但这应该会导致错误消息,而不是这种不必要的行为。
<?php
require_once('authorize.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Guitar Wars - High Scores Administration</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<h2>Guitar Wars - High Scores Administration</h2>
<p>Below is a list of all Guitar Wars high scores. Use this page to remove scores as needed.</p>
<?php
require_once('appvars.php');
require_once('connectvars.php');
// Connect to the database
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
// Retrieve the score data
$query = "SELECT * FROM guitarwars ORDER BY score DESC, date ASC";
$data = mysqli_query($dbc, $query);
// Loop through the array of more data, formatting it as HTML
echo '<table>';
while ($row = mysqli_fetch_array($data)) {
// Display the score data
echo '<tr class="scorerow"><td><strong>' . $row['name'] . '</strong></td>';
echo '<td>' . $row['date'] . '</td>';
echo '<td>' . $row['score'] . '</td>';
echo '<td><a href="removescore.php?id=' . $row['id'] . '&date=' . $row['date'] .
'&name=' . $row['name'] . '&score=' . $row['score'] . '&screenshot=' . $row['screenshot'] .
'">Remove</a></td></tr>';
}
echo '</table>';
mysqli_close($dbc);
?>
</body>
</html>