[英]php login form password invalid message problems
I have a login form on index.php page, I have a login.php file to connect and check things with MySQL database. 我在index.php页面上有一个登录表单,我有一个login.php文件来连接和检查MySQL数据库。 If the password or username is wrong, I would like to go back to the form and display an error message. 如果密码或用户名错误,我想返回表格并显示错误消息。
Currently, the code in login.php after submission is like this: 当前,提交后login.php中的代码如下:
//database checking...
$num_row = mysql_num_rows($result);
if( $num_row == 1 ) {
$_SESSION['login_user']=$username;
header("location:startscreen.php");
}
else{
$_SESSION['login_error'] = "Your username or password is incorrect";
header("location:index.php");
exit();
}
and in the index.php page: 并在index.php页面中:
<?php
if (! empty($_SESSION('login_error'])) {
echo '<div>'.$_SESSION('login_error').'</div>';
}
else {echo '<div>empty error msg</div>';}
?>
Now the situation is: When the password is correct, the page flows correctly, but if the password is wrong, it goes back to the form, but the $_SESSION['login_error'] is actually empty. 现在的情况是:密码正确时,页面将正确流动,但是如果密码错误,它将返回到表单,但是$ _SESSION ['login_error']实际上为空。 Is that something obviously wrong here? 这显然有问题吗? I can't work out why it didn't work. 我不知道为什么它不起作用。
Thanks for help! 感谢帮助!
Add session_start(); 添加session_start(); on top of your script 在脚本之上
And replace your code with the code below in their respective files. 并将您的代码替换为各自文件中的以下代码。 This is more efficient way of coding. 这是更有效的编码方式。
login.php login.php
//database checking...
$num_row = mysql_num_rows($result);
if( $num_row ) {
$_SESSION['login_user']=$username;
header("location:startscreen.php");
} else{
$_SESSION['login_error'] = "Your username or password is incorrect";
header("location:index.php");
exit();
}
index.php index.php
if ( isset($_SESSION['login_error']) and !empty($_SESSION['login_error']) ) {
echo '<div>'.$_SESSION('login_error').'</div>';
} else {
echo '<div>empty error msg</div>';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.