请教一下:我这个代码为什么提交了错误的密码之后,没有错误提示啊。
按道理不是应该提示:对不起,用户名或密码错误,登录失败!
<?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设置失败';
}
}
} else {
echo '对不起,用户名或密码错误,登录失败!';
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>用户登录</title>
</head>
<body>
<form method="post" action="login.php" >
姓名:<input type="text" name="username" />
密码:<input type="password" name="password" />
<input type="submit" name="submit" value="登录" />
</form>
</body>
</html>
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
if(isset($_POST['submit'])){
......
} else {
echo '对不起,用户名或密码错误,登录失败!';
}
你的else判断的是未定义$_POST['submit']变量时的情况。。。
else {
echo '对不起,用户名或密码错误,登录失败!';
}
你这个是判断if(isset($_POST['submit']))这个是否的,并不是判断账号密码的正确,所以不显示
<?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 '对不起,用户名或密码错误,登录失败!'; } ?>