问题描述
我的会话脚本遇到问题.我需要在其中进行会话的每个文件中都包含此文件调用functions.php.
I am having trouble with my session script. I include this file call functions.php in every file I need a session in.
<?php
session_start(); {
if(isset($_SESSION['username']) && !empty($_SESSION['username'])) {
return true;
} else {
return false;
}
}
?>
然后我使用此文件注销.叫做logout.php
And then I use this file to logout. Called logout.php
<?php
include('functions.php');
session_destroy();
// We redirect them to the login page
header("Location: homepage.php");
die("Redirecting to: homepage.php");
?>
任何人都可以帮助我修复此问题,以便当用户单击注销链接时,他们无法返回到成员区域并再次登录.
Can anyone help me fix it so that when a user clicks the logout link they cannot go back to the members area and be logged in again.
推荐答案
好的,我想问题是这样的,您只需在logout.php中破坏会话,但不清除会话变量.请查看文档,
Ok, I assume the problem was this,You just destroy the session within the logout.php, but not clearing the session variables. Please take a look at the documentation,
您遇到的情况是,每当您返回首页时,您都将重新启动会话,因此您可以访问$_SESSION['username']
,因为您没有清除变量并登录.
What happened in your case is, whenever you going back to the home page, you restart the session, therefore you will be able to access the $_SESSION['username']
since you did not clear the variable and you get logged in.
您的问题的解决方案是
<?php
include('functions.php');
session_unset(); // need to be called before session_destroy()
session_destroy();
// We redirect them to the login page
header("Location: homepage.php");
die("Redirecting to: homepage.php");
?>
,或者您只需在logout.php脚本中清除$_SESSION['username']
,就不必完全销毁会话.
or you can simply clear the $_SESSION['username']
within logout.php script, and you don't necessarily need to destroy the session at all.
希望这会有所帮助
这篇关于PHP会话:后退按钮问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!