dongzhen4180 2010-12-23 09:37
浏览 98
已采纳

虽然用户名和密码正确并保存在数据库中,但密码不正确仍会出现

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en" >
<head>
 <title>Login | JM Today </title>
 <link href="Mainstyles.css" type="text/css" rel="stylesheet" />
</head>
<body>
<div class="container">
 <?php include("header.php"); ?>
 <?php include("navbar.php"); ?>
 <?php include("cleanquery.php") ?>  

 <div id="wrap">


  <?php 
  $check=checklogin();
  if($check == true){
   confirmcookie($_SESSION['username'], $_SESSION['password']);
  } 

  ?>

  <?php

  $conn=mysql_connect("localhost", "***", "***") or die(mysql_error());
  mysql_select_db('jmtdy', $conn) or die(mysql_error());

  function checklogin() {
   if(isset($_COOKIE['cookname']) && isset($_COOKIE['cookpass'])){

        $_SESSION['username'] = $_COOKIE['cookname'];
        $_SESSION['password'] = $_COOKIE['cookpass'];
        return true;
      }

      elseif(isset($_POST['sublogin'])){
       if((strlen($_POST['user']) > 3) && (strlen($_POST['pass']) > 3)){
        $user=cleanQuery($_POST['user']);
        $pass=cleanQuery($_POST['pass']);
        $result=mysql_query("select password from users where username='$user' and active='1'");

        if(mysql_num_rows(mysql_query("select username from users where username='$user' and active = '1'" ) < 1)){
         $msg='<p class="statusmsg">The username you entered is incorrect, or you haven\'t yet activated your account. Please try again.</p><br/>';
         $status="NOTOK";
        }
       }

       else{
        $msg=$msg.'<p class="statusmsg">You didn\'t fill in the required fields.</p><br/>';
        $status="NOTOK";
       } 


       if($status != "NOTOK"){

        if(mysql_num_rows(mysql_query("select password from users where password='$pass'" ))){
      $_SESSION['username']=$user;
         $_SESSION['password']=$password;
        }
        else{
         $msg=$msg.'<p class="statusmsg">The password you entered is incorrect.</p>';
         $status="NOTOK";

         }

        if(isset($_POST['remember'])){
         setcookie("cookname", $_SESSION['username'], time()+60*60*24*100, "/");
          setcookie("cookpass", $_SESSION['password'], time()+60*60*24*100, "/");
         }
        }
        if($status=="NOTOK"){
         echo $msg . '<input type="button" value="Retry" onClick="location.href='."'login.php'\">";
         }
        else{
         echo '<p class="statusmsg"> You have succesfully logged in! </p>'; 
         }
       }
       else{
        echo '<p class="statusmsg"> You came here by mistake, didn\'t you? </p>';
       }

      } 


      function confirmcookie($username, $password){

     $result = mysql_query("select password from users where username = '$username'");

     if(!$result || (mysql_num_rows($result) < 1)){
      unset($_SESSION['username']);
      unset($_SESSION['password']);
        $msg='<p class="statusmsg">The username you entered is incorrect or you have set an incorrect cookie</p>';
        $status=="NOTOK";
     }

     $dbarray = mysql_fetch_array($result);
     $dbarray['password']  = cleanQuery($dbarray['password']);
     $password = cleanQuery($password);

     if($password!=$dbarray['password']){
        unset($_SESSION['username']);
        unset($_SESSION['password']);
        $msg='<p class="statusmsg">The password you entered is incorrect or you have set an incorrect cookie.</p>'; 
     }
     if(count_chars($msg) >0){
      echo $msg;
      }
  }



  ?>
  </div>
  <br/>
  <br/>
<?php include("footer.php") ?>
</div>
</body>

</html>

and here is the cleanQuery function:

<?php
function cleanQuery($string)
{
  if(get_magic_quotes_gpc())  // prevents duplicate backslashes
  {
    $string = stripslashes($string);
  }
  if (phpversion() >= '4.3.0')
  {
    $string = mysql_real_escape_string($string);
  }
  else
  {
    $string = mysql_escape_string($string);
  }
  return $string;
}

?>
  • 写回答

2条回答 默认 最新

  • douchi1945 2010-12-23 09:42
    关注

    Your calling checklogin() before it gets a chance to actually connect to the database!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。