在 PHP 中更新密码后在登录页面中显示消息

Displaying message in login page after updating password in PHP

我有一个工作正常的密码更新系统。但是,问题是我无法显示任何像 'Password Changed!' 这样的成功消息。因为,密码更新后,我的系统将用户重定向到注销页面,而注销页面又重定向到登录页面。我只想在密码更新后在登录页面中显示此消息。我无法尝试从 $_SESSION 变量中放置和捕获消息,因为注销会话被破坏时。我将不胜感激关于这个话题的任何好主意。提前致谢。

一些想法:

1) 使用 Cookie 而不是 $_SESSION(它确实会在启动新会话后被销毁)。如果设置了cookie,显示你显示的消息(并删除co​​okie)

2) 不要立即重定向到登录页面,而只是说:"Password changed! Click here to log in again."。但是您可以安全地销毁该页面上的会话。

3) 根本不要破坏会话。密码已更改的事实并不意味着您必须破坏会话。如果您的会话包含类似以下内容:$_SESSION['validUser'] = 'Y' 这可能就足够了吗?

Erwin Moller 的建议都是很好的建议,而且都很容易实施。我认为这些是最好的方法,但是如果您不想使用这些方法中的任何一种,那么您可以在一条普通消息中将 $_GET 变量从一个重定向中继到另一个重定向或表示消息的值:

$red_url = 'http://www.example.com/?msg=Password+has+been+reset';
header("Location: {$red_url}");
exit;

确保在输出消息时使用类似 htmlspecialchars() 或等效的内容。这会处理恶意代码插入,但此方法仍然允许最终用户最灵活地扰乱您的网站消息传递。

变量方法类似:

$red_url = 'http://www.example.com/?msg=1234';
header("Location: {$red_url}");
exit;

在显示页面上使用某种方式获取或翻译 msg 代码:

<?php if(!empty($_GET['msg']) && is_numeric($_GET['msg'])): ?>
<h3 class="site-message"><?php echo myCodeToMessageFunc($_GET['msg']) ?></h3>
<?php endif ?>

最可能发生的情况是用户可以手动中继不同的代码以显示不同的消息。

** These 2 methods can be messed around with by the user (harmlessly if you are scrubbing or controlling the output) so that is why a cookie or session is better, but this is a pretty straight forward messaging method.