【问题标题】:Page authentication doesn't work页面身份验证不起作用
【发布时间】:2011-09-27 10:38:23
【问题描述】:

此代码来自 O'Reilly Head First PHP&MySQL 书。该脚本无法以某种方式识别用户名和密码:

    <?php
  $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.');
  }
?>

这是需要验证文件的脚本:

 <?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>
  <hr />

<?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 from MySQL
  $query = "SELECT * FROM guitarwars ORDER BY score DESC, date ASC";
  $data = mysqli_query($dbc, $query);

  // Loop through the array of score data, formatting it as HTML 
  echo '<table>';
  echo '<tr><th>Name</th><th>Date</th><th>Score</th><th>Action</th></tr>';
  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'] . '&amp;date=' . $row['date'] .
      '&amp;name=' . $row['name'] . '&amp;score=' . $row['score'] .
      '&amp;screenshot=' . $row['screenshot'] . '">Remove</a>';
    if ($row['approved'] == '0') {
      echo ' / <a href="approvescore.php?id=' . $row['id'] . '&amp;date=' . $row['date'] .
        '&amp;name=' . $row['name'] . '&amp;score=' . $row['score'] . '&amp;screenshot=' .
        $row['screenshot'] . '">Approve</a>';
    }
    echo '</td></tr>';
  }
  echo '</table>';

  mysqli_close($dbc);
?>

</body> 
</html>

你能看出这里有什么问题吗? 谢谢!

【问题讨论】:

标签: php mysql html


【解决方案1】:

验证码完全没问题。

在header() 调用之前,您可能有额外的空格(在&lt;?php 之前)或其他内容。在这种情况下,您看不到身份验证弹出窗口。

【讨论】:

  • 我确实看到它弹出了,但是每次我输入用户名和密码时,它都不接受这些并再次弹出。
  • 应该工作.. 大写锁定?你试过 print_r($_SERVER) 吗?
  • 这就是答案吗? - discussion.dreamhost.com/thread-71152.html我在 Dreamhost。当我返回 $_SERVER 数组时,PHP_AUTH_USER 和 PHP_AUTH_PW 甚至不在列表中。
  • 如果是这样,就离开他们吧.. 我的意思是.. CGI? ://
  • 好的,Dreamhost 的技术支持确认变量不起作用,因为 PHP 作为 CGI(或 fastCGI)运行。切换到 mod_php 意味着更昂贵的托管。现在,我决定尝试使用其他一些身份验证方法。非常感谢您的帮助!
猜你喜欢
  • 2011-06-23
  • 1970-01-01
  • 2017-04-10
  • 2012-10-04
  • 1970-01-01
  • 2012-04-29
  • 2012-03-18
相关资源
最近更新 更多