如何在点击注销后完全结束PHP会话?

huangapple go评论113阅读模式
英文:

How to completely end a session in PHP after clicking Logout?

问题

现在我有一个网站,当我点击退出按钮时,它会将我重定向到登录页面,但如果我更改浏览器的URL为menu.php,例如,我发现用户仍然在线,我不知道如何完全终止会话。

退出按钮将用户重定向到logout.php,其中包含以下代码

<?php 
session_unset();
session_destroy();

header("Location: index.php");
exit();
?>

我当时在menu.php页面,然后点击了退出按钮。它将我重定向到了index.php,就像应该的那样。但当我将浏览器的URL从index.php更改为menu.php时,我发现欢迎消息中仍然包含上一个用户名!

menu.php应该在您点击退出后不打开,除非您再次登录。

英文:

Now I have a website , when I press log out button it return me to the login page, but if I change the url of the browser to menu.php for an example, I found the user is still on , I don't know how do I kill the session completely .

logout button will direct the user to logout.php which has the following code

&lt;?php 
session_unset();
session_destroy();

header(&quot;Location: index.php&quot;);
exit();
?&gt;

I was in menu.php and I click on Logout button. It direceted me to index.php as it's suppose to be. When I changed the url of the browser from index.php to menu.php I found the welcome message containing the last user name in!

menu.php suppose to not open if you click logout and did not sign in again

答案1

得分: 0

我终于找到了。
说我必须在结束会话之前开始会话的那个人是对的。

这是新的logout.php代码:

<?php 

session_start();

session_unset();
session_destroy();

header("Location: index.php");

?>
英文:

I finally found it out.
The guy who said that I must start the session before I end it was right

this is the new logout.php code

&lt;?php 

session_start();

session_unset();
session_destroy();

header(&quot;Location: index.php&quot;);

?&gt;

huangapple
  • 本文由 发表于 2023年5月29日 20:41:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76357478.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定