du13932014807 2012-05-09 04:01
浏览 29
已采纳

if和else格式? PHP

I've been trying to figure out what I'm doing wrong. When it checks if the user is activated on line 26 even if the user is activated it sends the user to line 38 which tells tells them their username or password is incorrect but they are correct. You can find the two lines on the left side of the code.

   <?php
      require("includes/inc.php");
      if ($_SESSION['username'] != null){
        # Redirect the user to the member area
        header('Location: member.php');
      } else {
        # Check if the user is trying to login
        if ($_GET['do'] == "login"){
          # If they are, process the details they have provided. Else, continue with showing the form
          $username = trim(sanitize($_POST['username']));
          $password = trim(sanitize($_POST['password']));
          # Check if the username and password are empty
          if (($username == null) || ($password == null)){
            header('Location: login.php?error=field_blank');
          } else {
            $query_accounts = mysql_query("SELECT * FROM users WHERE `username` = '$username' LIMIT 1");
            $query_count = mysql_num_rows($query_accounts);
            if ($query_count == null){
              // User not found
              header('Location: login.php?error=details_wrong');
            } else {
//Line 26          $active = mysql_fetch_array($query_accounts);
                if ($active['active'] == 0) {
                    header('Location: login.php?error=activate');
                } else {
                   $accounts = mysql_fetch_array($query_accounts);
                    // Check if the password matches the user's password
                     if ($accounts[password] == password($password)){
                    // The password is correct, start a session for the user
                        $_SESSION['username'] = $username;
                        header('Location: member.php');
                    } else {
                    // Incorrect password
//Line 38                   header('Location: login.php?error=details_wrong');
                }
              }
            }
          }
        } else {
    ?>
    <!doctype html>
    <html>
    <head>
    <title>PHP Login & Registration</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="css/style.css" />
    <div id="main">  
    <h1>Login</h1>
    </head>
    <body>
        Need a account? <a href="register.php">Register</a>
        <!-- Display Messages -->
        <?php
          # -> Messages
          if ($_GET['error'] == "field_blank"){ echo "<div class='error'>The username and/or password field was left blank</div>
"; }
          elseif ($_GET['error'] == "details_wrong"){ echo "<div class='error'>The username and/or password was incorrect</div>
"; }
          elseif ($_GET['error'] == "activate"){ echo "<div class='error'>Please activate your account.</div>
"; }
          elseif ($_GET['success'] == "logout"){ echo "<div class='success'>You are now logged out</div>
"; }
          elseif ($_GET['success'] == "complete"){ echo "<div class='success'>You are now registered, please activate your account by visiting your email.
"; }
        ?>

          <!-- Login Form -->
          <form action="?do=login" method="post" autocomplete="on">
            <fieldset>
            <p>Username</p>
            <input type="text" name="username" size="40" maxlength="20" /> <br />
            <p>Password</p>
            <input type="password" name="password" size="40" maxlength="30" /> <br />
            <input type="submit" value="Login" style="width:80px;" />
            </fieldset>
        <?php include "footer.php"; ?>
          </form>
    </div>
    </body>
    </html>
    <?php
        } // End Check Login
      } // End check if logged in
    ?>
  • 写回答

6条回答 默认 最新

  • dongqidi2799 2012-05-09 04:17
    关注

    i think you have problem with $accounts = mysql_fetch_array($query_accounts);

    $accounts is array that contain row and column as index you need to use while

    while($accounts = mysql_fetch_array($query_accounts))
    {
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料