【发布时间】:2021-02-08 23:50:10
【问题描述】:
我需要在输入无效的用户名或密码时显示消息。
我进行了设置,以便在发生某些事情时将页面重定向回index.php 页面,但添加了?message=Bad_Username 或?message=Bad_Password。
我还设置了一个 if 语句,以便在满足某些条件时,我可以在用户名或密码字段下方打印消息。
我只是不确定 If 语句需要什么条件才能工作。
这是代码。
if (($_GET['username'])){
echo "<p class='help is-danger'>This username is invalid</p>";
if(header("location: index.php?message=Bad_Username")){
echo "<p class='help is-danger'>This username is invalid</p>";
}
}
【问题讨论】:
-
$_GET只是一个数组。知道了这一点,您可以检查键和特定值的存在。if (isset($_GET['message']) && $_GET['message'] === 'Bad_Username')) { ... -
if(header... 完全没有意义。根据php.net/manual/en/function.header.php,header()不返回值,因此对if进行评估没有任何用处。即使确实如此,也不清楚您要通过这样的声明来实现什么。设置重定向标头告诉浏览器忽略当前响应并转到另一个页面。因此,在当前响应中回显某些内容是没有意义的,因为您刚刚告诉浏览器忽略它。
标签: php if-statement message feedback