dsf6778 2017-01-25 15:57
浏览 75
已采纳

登录接受并失败?

Hello everybody I am trying to make a login script in PHP.. it works well i am able to login but i got this strange bug or whatever to call it... you see when i login as an ordinary user it works fine, but when i login as admin i get loged in but in the same time it says my login failed...

I got this welcome message:

<?php echo "<h3 id ='tjena'> Welcome ".$_SESSION['user']."</h3>";?>

so i know that i am actully logged in... However i also got a header which are supposed to lead me to ?success but of some reason it fails and directs me to ?error

Here is my code:

while($row = $result->fetch_object()) {
    if($username == $row->username) {
        $checkPassword = password_verify($password,$row->password);
        if($checkPassword ){   
            session_start();
            $_SESSION['loggedIn'] = true; 
            $_SESSION['user'] = $row->username;
            $_SESSION['admin'] = $row->admin;
            $_SESSION['LAST_ACTIVITY'] = time(); 
            header("Location:index.php?success");
            $fail = false;
        }
    } else {
        $fail = true;
    }
}

if($fail){      
    header("Location:index.php?error");
}

Does somebody know what is causing this error? Thans in advance!

  • 写回答

2条回答 默认 最新

  • dopnpoh056622 2017-01-25 16:19
    关注

    The PHP script does redirect the user to another page, but that script is not stopping its execution, unless you tell it to.

    That is why i think adding a line with exit(); will do the trick.

    while($row = $result->fetch_object()) {
        if($username == $row->username) {
            $checkPassword = password_verify($password,$row->password);
            if($checkPassword ){   
                session_start();
                $_SESSION['loggedIn'] = true; 
                $_SESSION['user'] = $row->username;
                $_SESSION['admin'] = $row->admin;
                $_SESSION['LAST_ACTIVITY'] = time(); 
                header("Location:index.php?success");
                exit();
                $fail = false;
            }
        } else {
            $fail = true;
        }
    }
    
    if($fail){      
        header("Location:index.php?error");
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?