Can';无法在PHP中获取错误消息
我有一个登录表单Can';无法在PHP中获取错误消息,php,include,Php,Include,我有一个登录表单index.php,它的输入是ID和密码 login\u user.php 它接受数据 如果凭据正确,则用户将正确路由到主页。但是,当用户输入错误的登录详细信息时,我必须检查数据库中存储的值。因此,我认为应该在login\u user.php中使用一些变量,然后将其包含在index.php中,并在div中打印错误。到目前为止,我做了这个代码 <div class="alert alert-warning alert-dismissible" role=&
index.php
,它的输入是ID和密码
login\u user.php
它接受数据
如果凭据正确,则用户将正确路由到主页。但是,当用户输入错误的登录详细信息时,我必须检查数据库中存储的值。因此,我认为应该在login\u user.php
中使用一些变量,然后将其包含在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>";
}
?>
此代码的作用是什么
加载index.php
时,将隐藏定义错误的div
。这个很好用
但当用户输入错误的登录输入时。然后div
也被设置为隐藏
我不明白我在哪里犯了错误。如果还需要什么,请询问,以便我能提供。提前感谢 使用标题('location:index.php')
代码立即重定向用户;一旦发生这种情况,$error
将在干净页面加载的情况下被清除,这意味着您的if(empty($error))
条件将始终计算为true
您将立即使用标题('location:index.php')
代码重定向用户;一旦发生这种情况,$error
将在干净页面加载的情况下被清除,这意味着您的if(empty($error))
条件将始终计算为true
您将立即使用标题('location:index.php')
代码重定向用户;一旦发生这种情况,$error
将在干净页面加载的情况下被清除,这意味着您的if(empty($error))
条件将始终计算为true
您将立即使用标题('location:index.php')
代码重定向用户;一旦发生这种情况,$error
将在干净页面加载的情况下被清除,这意味着您的if(empty($error))
条件将始终计算为true
我个人使用$\u会话变量通过PHP脚本管理错误
你的div是
{$\u会话['error']}
在你展示它之后就杀了它
这是herdoc语法。对不起,如果你不习惯的话
这样,您就不必管理show/hide div,因为如果$\u 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');
}
我个人使用$\u会话变量通过PHP脚本管理错误
你的div是
{$\u会话['error']}
在你展示它之后就杀了它
这是herdoc语法。对不起,如果你不习惯的话
这样,您就不必管理show/hide div,因为如果$\u 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');
}
我个人使用$\u会话变量通过PHP脚本管理错误
你的div是
{$\u会话['error']}
在你展示它之后就杀了它
这是herdoc语法。对不起,如果你不习惯的话
这样,您就不必管理show/hide div,因为如果$\u 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');
}
我个人使用$\u会话变量通过PHP脚本管理错误
你的div是
{$\u会话['error']}
在你展示它之后就杀了它
这是herdoc语法。对不起,如果你不习惯的话
这样,您就不必管理show/hide div,因为如果$\u 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');
}
您在这两种情况下都设置了$error
,因此empty($error)
将永远不会为真。打印输出后不能调用header()
。在包含login\u user.php
之前,请先打印
,这样那些标题调用就不会起任何作用。@Barmar,但如果用户输入错误,我需要重定向到login.php
页面。所以我用这个header()
我明白这一点。您只需重新组织代码,这样它就不会首先打印DIV。@xNeyte如果用户输入了正确的ID但密码错误,那么我必须告诉用户输入了错误的密码。因此我使用了此选项。您在这两种情况下都设置了$error
,因此empty($error)
将永远不会为真。您无法调用header()
打印输出后。在包含login\u user.php
之前,请先打印
,这样那些标题调用就不会起任何作用。@Barmar,但如果用户输入错误,我需要重定向到login.php
页面。所以我用这个header()
我明白这一点。您只需重新组织代码,这样它就不会首先打印DIV。@xNeyte如果用户输入了正确的ID但密码错误,那么我必须告诉用户输入了错误的密码。因此我使用了此选项。您在这两种情况下都设置了$error
,因此empty($error)
将永远不会为真。您无法调用header()
打印输出后。在包含login\u user.php
之前,请先打印
,这样那些标题调用就不会起任何作用。@Barmar,但如果用户输入错误,我需要重定向到login.php
页面。所以我使用这个header()
我理解