drzil26260 2012-07-10 09:59
浏览 61
已采纳

为什么即使在第一次加载页面时出现“错误的用户名/密码”警报?

I have a login form and when I get to the page, it gives me automatically the alert message. I havn't put the username and password info yet.
Here is the page that does the verification of the login form input, Please see why it is behaving like that?

<?
session_start();
include "config.php";

if(isset($_SESSION["login_user"]) AND isset($_SESSION['pass_user'])){
    $login_user = $_SESSION["login_user"];
    $pass_user = $_SESSION["pass_user"];

$sql = mysql_query("SELECT * FROM adm WHERE login = '$login_user'");
$cont = mysql_num_rows($sql);
while($linha = mysql_fetch_array($sql)){
    $pass_db = $linha['pass'];
}

if($cont == 0){
    unset($_SESSION["login_user"]);
    unset($_SESSION["pass_user"]);

    echo "
    <META HTTP-EQUIV=REFRESH CONTENT='0; URL=login.php'>
    <script type=\"text/javascript\">
    alert(\"Name of user dont match.\");
    </script>";

}

if($pass_db != $pass_user){//check pass

    unset($_SESSION["login_user"]);
    unset($_SESSION["pass_user"]);

    echo "
    <META HTTP-EQUIV=REFRESH CONTENT='0; URL=login.php'>
    <script type=\"text/javascript\">
    alert(\"Password dont match.\");
    </script>";

}

}else{

    echo "
    <META HTTP-EQUIV=REFRESH CONTENT='0; URL=login.php'>
    <script type=\"text/javascript\">
    alert(\"User and password dont match.\");
    </script>";

}

?>
  • 写回答

3条回答 默认 最新

  • douwu5428 2012-07-10 10:07
    关注

    I guess you have just misplaced an else:

    <?
    session_start();
    
    include "config.php";
    
    if(isset($_SESSION["login_user"]) AND isset($_SESSION['pass_user'])){
    
        $login_user = $_SESSION["login_user"];
        $pass_user = $_SESSION["pass_user"];
    
    $sql = mysql_query("SELECT * FROM adm WHERE login = '$login_user'");
    $cont = mysql_num_rows($sql);
    while($linha = mysql_fetch_array($sql)){
        $pass_db = $linha['pass'];
    }
    
    if($cont == 0){
    
        unset($_SESSION["login_user"]);
        unset($_SESSION["pass_user"]);
    
        echo "
        <META HTTP-EQUIV=REFRESH CONTENT='0; URL=login.php'>
        <script type=\"text/javascript\">
        alert(\"Name of user dont match.\");
        </script>";
    
    }
    
    if($pass_db != $pass_user){//check pass
    
        unset($_SESSION["login_user"]);
        unset($_SESSION["pass_user"]);
    
        echo "
        <META HTTP-EQUIV=REFRESH CONTENT='0; URL=login.php'>
        <script type=\"text/javascript\">
        alert(\"Password dont match.\");
        </script>";
    
    }else{
    
        echo "
        <META HTTP-EQUIV=REFRESH CONTENT='0; URL=login.php'>
        <script type=\"text/javascript\">
        alert(\"User and password dont match.\");
        </script>";
    
    }
    }
    ?> 
    

    However, As mentioned by @David, you should not specify that only password is wrong.

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

报告相同问题?

悬赏问题

  • ¥15 visionmaster启动失败,提示为“机器不满足授权而被禁用”
  • ¥50 用logisim设计16位单时钟周期cpu
  • ¥15 IDEA中圈复杂度如何具体设置
  • ¥50 labview采集不了数据
  • ¥15 请上面代码做什么处理或什么混淆
  • ¥15 英雄联盟自定义房间置顶
  • ¥15 W5500网线插上无反应
  • ¥15 如何用字典的Key,显示在WPF的xaml中
  • ¥15 weautomate读取Excel表格信息然后填写到网页一直报错,如何解决?
  • ¥15 C#如何在Webview2中获取网页验证码