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

登录接受并失败?

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!

图片转代码服务由CSDN问答提供 功能建议

大家好我想在PHP中创建一个登录脚本..它运行良好我可以登录但是我 得到这个奇怪的错误或任何称之为...你看到我作为一个普通用户登录它工作正常,但当我以管理员身份登录时,我得到了,但在同一时间它说我的登录失败...

我收到了这条欢迎信息:

 &lt;?php echo“&lt; h3 id ='tjena'&gt; Welcome”。$ _ SESSION  ['user']。“&lt; / h3&gt;”;?&gt; 
   
 
 

所以我知道我已经登录... 但是我也是 得到了一个标题,它应该引导我走向成功,但由于某种原因它会失败并指示我?错误

这是我的代码:

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

有人知道导致此错误的原因是什么吗? 提前完成!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dopnpoh056622
    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");
    }
    
    点赞 评论
  • dongnao3990
    dongnao3990 2017-01-25 16:29

    Where is the code that checks if you are a normal user or admin? You have $_SESSSION['user'] and $_SESSSION['admin'] if $checkpassword is true. session_start(); should definitely be put at the very top + write a simple if statement to check for user or admin.

    • you should put $fail = false; before the header("Location: index.php?success") statement.
    点赞 评论

相关推荐