无法在 PHP 中收到错误消息
Can't get Error message in PHP
我有一个登录表单 index.php
,其中输入了 ID,Password.I 获取输入并将其提交到另一个名为
login_user.php
接受数据。
如果凭据正确,则用户将正确路由到主页。但是当用户输入错误的登录详细信息时,我必须检查 DB.So 中的存储值,我认为在 login_user.php
中使用一些 variable
然后 include
它在 index.php
中并在 div
中打印错误。到目前为止,我制作了这段代码。
<div class="alert alert-warning alert-dismissible" role="alert" id="alert_login_error">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<strong id="error_flag">
Invalid Login Details
</strong>
</div>
<?php
include "login_user.php";
if (empty($error)) {
echo "<script> $('#alert_login_error').hide(); console.log('empty');</script>";
}
else {
echo "<script> $('#alert_login_error').show();console.log('not empty');</script>";
}
?>
并且 login_user.php
文件有这个
$error = ""; #this is set at the start of the file
$num_rows = mysql_num_rows($result);
if ($num_rows > 0) {
#User is redirected to Home Page if details are correct
#If the password is wrong then in a function
$error = "Invalid password";
header('Location: index.php');
}
else {
$error = "Invalid Login details";
header('Location: index.php');
}
此代码的作用
When index.php
is loaded the div
in which error is defined is hide. This works fine.
but when user inputs wrong login inputs. then also the div
is set to hide
我不明白我在做什么mistake.If还有什么需要请问,这样我就可以提前provide.Thanks!
您正在使用 header('location: index.php')
代码立即重定向用户;一旦发生这种情况,$error
就会在干净的页面加载时被清除干净,这意味着您的 if (empty($error))
条件将始终评估为 true
.
我个人使用 $_SESSION 变量通过 PHP 脚本管理错误。
你的 div 会是
<div id='error'> {$_SESSION['error']} </div>
然后你一显示就杀了
这是 heredoc 语法。如果您不习惯,请见谅。
这样,您就不必管理 show/hide div,因为如果 $_SESSION['error']
为空
,div 将为空
注意:我不知道这是否是一个好的解决方案,但它至少是一个可行的解决方案!
因此您的 PHP 脚本将是:
if ($num_rows > 0) {
#User is redirected to Home Page if details are correct
#If the password is wrong then in a function
$_SESSION['error'] = "Invalid password";
header('Location: index.php');
}
else {
$_SESSION['error'] = "Invalid Login details";
header('Location: index.php');
}
我有一个登录表单 index.php
,其中输入了 ID,Password.I 获取输入并将其提交到另一个名为
login_user.php
接受数据。
如果凭据正确,则用户将正确路由到主页。但是当用户输入错误的登录详细信息时,我必须检查 DB.So 中的存储值,我认为在 login_user.php
中使用一些 variable
然后 include
它在 index.php
中并在 div
中打印错误。到目前为止,我制作了这段代码。
<div class="alert alert-warning alert-dismissible" role="alert" id="alert_login_error">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<strong id="error_flag">
Invalid Login Details
</strong>
</div>
<?php
include "login_user.php";
if (empty($error)) {
echo "<script> $('#alert_login_error').hide(); console.log('empty');</script>";
}
else {
echo "<script> $('#alert_login_error').show();console.log('not empty');</script>";
}
?>
并且 login_user.php
文件有这个
$error = ""; #this is set at the start of the file
$num_rows = mysql_num_rows($result);
if ($num_rows > 0) {
#User is redirected to Home Page if details are correct
#If the password is wrong then in a function
$error = "Invalid password";
header('Location: index.php');
}
else {
$error = "Invalid Login details";
header('Location: index.php');
}
此代码的作用
When
index.php
is loaded thediv
in which error is defined is hide. This works fine.but when user inputs wrong login inputs. then also the
div
is set to hide
我不明白我在做什么mistake.If还有什么需要请问,这样我就可以提前provide.Thanks!
您正在使用 header('location: index.php')
代码立即重定向用户;一旦发生这种情况,$error
就会在干净的页面加载时被清除干净,这意味着您的 if (empty($error))
条件将始终评估为 true
.
我个人使用 $_SESSION 变量通过 PHP 脚本管理错误。
你的 div 会是
<div id='error'> {$_SESSION['error']} </div>
然后你一显示就杀了
这是 heredoc 语法。如果您不习惯,请见谅。
这样,您就不必管理 show/hide div,因为如果 $_SESSION['error']
为空
注意:我不知道这是否是一个好的解决方案,但它至少是一个可行的解决方案!
因此您的 PHP 脚本将是:
if ($num_rows > 0) {
#User is redirected to Home Page if details are correct
#If the password is wrong then in a function
$_SESSION['error'] = "Invalid password";
header('Location: index.php');
}
else {
$_SESSION['error'] = "Invalid Login details";
header('Location: index.php');
}