I am new to PHP, why is there no error message after submitting the wrong password in my code?
戢运来²º¹7
戢运来²º¹7 2017-11-26 13:21:54
0
4
1238

Ask me: Why is there no error message after submitting the wrong password in my code?

Logically speaking, it should not prompt: Sorry, the username or password is wrong, login failed!

<?php

if(isset($_POST['submit'])){

if(isset($_POST['username']) && isset ($_POST['password']) && $_POST['username']==='sunshengli' && $_POST['password']==='123456') {

if(setcookie(' username',$_POST['username'],time() 3600)) {

header('Location:http://www.sifangku.com/');

} else {

echo 'Cookie setting failed';

}

}

} else {

echo 'Sorry, username or Incorrect password, login failed! ';

}

?>

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8" />

<title>User login</title>

</head>

<body>

<form method="post" action="login.php" >

Name:<input type="text" name="username " />

Password:<input type="password" name="password" />

<input type="submit" name="submit" value=" Login" />

</form>

</body>

</html>


戢运来²º¹7
戢运来²º¹7

reply all(3)
蔚蓝的夜

if(isset($_POST['submit'])){

......

} else {

echo 'Sorry, username Or the password is wrong and login failed! ';

}

Your else determines the situation when the $_POST['submit'] variable is not defined. . .

小丘

else {

echo 'Sorry, the username or password is wrong, login failed! ';

}

You are judging if (isset($_POST['submit'])), not whether the account password is correct, so it is not displayed

ringa_lee
<?php 
if(isset($_POST['submit'])){

if(isset($_POST['username']) && isset($_POST['password']) && $_POST['username']==='sunshengli' 
&& $_POST['password']==='123456') {//账号密码正确执行的地方

if(setcookie('username',$_POST['username'],time()+3600)) {
header('Location:www.sifangku.com/');
} else {
echo 'cookie设置失败';exit;
}

}//这里添加密码错误执行的地方,但是你没有任何操作,当然也就不提示了。
    //你可以加个else{ echo "账号密码错误";}


} else {
echo '对不起,用户名或密码错误,登录失败!';
}
?>


  • reply if(isset($_POST['submit'])){ ... } else { echo 'Sorry, the username or password is wrong, login failed! '; } Your else determines the situation when the $_POST['submit'] variable is not defined. . .
    蔚蓝的夜 author 2017-11-27 11:15:52
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template