-2

我在使用 PHP 进行登录验证时遇到了一些问题。我正在从数据库中选择正确的数据(已使用 phpmyadmin 检查过),如果我回显 SELECT*,则显示的数据是正确的。

但是,当我输入正确的登录详细信息时,脚本会跳到错误消息“对不起,您必须输入有效的用户名和密码才能登录”。

我已经搜索了我能想到的一切来尝试解决这个问题(打印查询结果等),但没有运气。代码如下:

<?php
    $link = mysql_connect('localhost:8889', 'root', 'root', 'help_me_be_healthy')
    or die('Could not connect: ' . mysql_error());

       //mysql_select_db('help_me_be_healthy') or die('Could not select database');

    $dbc = mysql_select_db('help_me_be_healthy', $link);
      if (!$dbc) {
        die("Database could not be selected" . mysql_error());
      }

  // Start the session
  session_start();

  // Clear the error message
  $error_msg = "";

  // If the user isn't logged in, try to log them in
  if (!isset($_SESSION['user_id'])) {
    if (isset($_POST['submit'])) {
      // Connect to the database
      $dbc = mysqli_connect('localhost:8889', 'root', 'root', 'help_me_be_healthy');


      echo one;

      // Grab the user-entered log-in data
      $username = mysqli_real_escape_string($dbc, trim($_POST['username']));
      //echo $_POST ['username'];
      $password = mysqli_real_escape_string($dbc, trim($_POST['password']));
      //echo $_POST ['password'];



      if (!empty($_POST['username']) && !empty($_POST['password'])){
        // Look up the username and password in the database

       $query = "SELECT `user_id`, `username` FROM `users` WHERE `username` = '$username' AND `password` = SHA('$password')";
       $data= mysql_query($dbc,$query);

        if (mysqli_num_rows($data) == 1) {
          // The log-in is OK so set the user ID and username session vars (and cookies), and redirect to the home page
          $row = mysqli_fetch_array($data);
          $_SESSION['user_id'] = $row['user_id'];
          $_SESSION['username'] = $row['username'];
          setcookie('user_id', $row['user_id'], time() + (60 * 60 * 24 * 30));    // expires in 30 days
          setcookie('username', $row['username'], time() + (60 * 60 * 24 * 30));  // expires in 30 days
          $home_url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/index.php';
          header('Location: ' . $home_url);

          echo four;
        }
        else {
          // The username/password are incorrect so set an error message
          $error_msg = 'Sorry, you must enter a valid username and password to log in.';

          echo five;
        }
      }
      else {
        // The username/password weren't entered so set an error message
        $error_msg = 'Sorry, you must enter your username and password to log in.';
      }
    }
  }
?>


<!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>Mismatch - Log In</title>
  <link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
  <h3>Mismatch - Log In</h3>

<?php
  // If the session var is empty, show any error message and the log-in form; otherwise confirm the log-in
  if (empty($_COOKIE['user_id'])) {
    echo '<p class="error">' . $error_msg . '</p>';
?>

  <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <fieldset>
      <legend>Log In</legend>
      <label for="username">Username:</label>
      <input type="text" id = "username" name="username" 
        value="<?php if (!empty($username)) echo $username; ?>" /><br />
      <label for="password">Password:</label>
      <input type="password" id = "password" name="password" />
    </fieldset>
    <input type="submit" value="Log In" name="submit" />
  </form>

<?php
  }
  else {
    // Confirm the successful log-in
    echo('<p class="login">You are logged in as ' . $_COOKIE['username'] . '.</p>');
  }
?>

</body>
</html>

任何帮助将非常感激!

4

3 回答 3

4

你正在混合mysql_querymysqli_num_rows。要么使用mysql_, OR mysqli_,但不能同时使用。它们不可互换。

于 2013-10-20T12:51:52.123 回答
0
$query = "SELECT `user_id`, `username` FROM `users` WHERE `username` = '$username' AND `password` = SHA('$password')";
       $data= mysql_query($dbc,$query);
    if (mysqli_num_rows($data) == 1) {

你的问题出在这个代码区域。尝试使用 mysql_num_rows 代替 mysqli,并尝试输出 mysql_num_rows() 的值,看是否为 1。

于 2013-10-20T12:53:47.490 回答
0

@[Niet the Dark Absol] 是正确的,你不能互换mysql_mysqli_.

有问题的代码是:

   $query = "SELECT `user_id`, `username` FROM `users` WHERE `username` = '$username' AND `password` = SHA('$password')";
   $data= mysql_query($dbc,$query);

更改mysql_querymysqli_query

您可以删除上面的数据库连接调用session_start()——它们不是必需的,因为您mysqli_connect()在检查用户是否已经登录后进行了非常好的调用。

于 2013-10-20T13:12:59.960 回答