目前手头有一个场景,我有2个页面index.php和code.php。
用户首先进入 index.php 页面,然后被重定向到 code.php 页面并返回到 index.php 页面。
如果 code.php 页面出现错误,我需要在 index.php 页面重定向回索引页面时显示该错误。
我像这样将错误消息存储在 session 中,然后将页面 bsck 重定向到 index.php 页面
$_SESSION["errormsg"]='please try again';
header("Location: index.php");
但是如果我在 index.php 页面中回显此 session 消息,它会在页面加载时随时显示,但是当索引页面出现在 code.php 页面之后时,我只想要一次。谁能告诉我怎么做
最佳答案
我认为,通过 session 显示错误消息的最佳方式是使用单独的消息文件,如 message.php。
第一步: 您只需在 code.php 中设置带有错误消息的 session 即可。
$_SESSION["errormsg"]='请重试';
第 2 步:
现在创建名为“message.php”的新文件。
将 session 错误消息存储到新变量中。
$error = $_SESSION["errormsg"];
第 3 步:
然后,取消设置 session 或销毁 session 。
// remove all session variables
session_unset();
// destroy the session
session_destroy();
第 4 步:
- 现在在格式化的 div 标签中回显该错误变量,这样您的错误看起来就不错,也很吸引人。
<div id='alert'><div class=' alert alert-block alert-info fade in center'>$error</div></div>
- 现在,在您的 index.php 文件中,有一个方法可以检查是否设置了 session 变量“errormsg”。
- 如果设置了 session 变量,则立即调用 message.php 文件。它会在你的 index.php 文件中回应那个 div。
我想这会给你完整的答案。
关于php - 在 session 中存储错误消息并显示在另一个页面上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28251555/