如何仅在适当时显示会话变量?

发布于 2025-01-25 05:57:22 字数 333 浏览 1 评论 0原文

我有一个PHP1页面,该页面将用户转移到PHP2页面,该页面设置了会话变量并将用户转移回PHP1页面。例如(登录表单显示错误消息) 现在,当我想在PHP1中显示该会话变量时,我尝试了以下操作:

if(isset($_SESSION['messages'])){
  echo($_SESSION['messages']); 
}

问题是即使用户仍设置了该变量,即使用户会刷新页面,它也会继续显示。 PS:我尝试使用Onload()和onunload()函数来取消设置,但它不起作用,但是我不确定是否犯了任何错误。

I have a php1 page that transfers the user to a php2 page which sets the session variable and transfers the user back to the php1 page. e.g.(login form displaying an error message)
Now when I wanted to display that session variable in php1 so I tried the following:

if(isset($_SESSION['messages'])){
  echo($_SESSION['messages']); 
}

the problem is that it will keep being displayed even if the user refreshes the page since the variable is still set.
ps: I tried to use onload() and onunload() functions to unset it but it did not work, but I'm not sure if I did any mistakes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾其所爱 2025-02-01 05:57:22

显示后删除会话变量。

if(isset($_SESSION['messages'])){
    echo($_SESSION['messages']); 
    unset($_SESSION['messages']);
}

Remove the session variable after you show it.

if(isset($_SESSION['messages'])){
    echo($_SESSION['messages']); 
    unset($_SESSION['messages']);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文