我知道这是一个常见的问题,但似乎不能让这个工作。
一个用户登录,一个会话和一个变量被添加。
用户退出,php脚本运行,会话被销毁。
退出后,我在index.php页面上打印会话,它是空的。
但是用户仍然可以看到登录页面,如果他们按回??
我使用下面的代码来销毁会话:session_start();
session_unset();
session_destroy();
session_write_close();
setcookie(session_name(),'',0,'/');
session_regenerate_id(true);
unset($_SESSION["username"]);
header("Location: index.php");
使用这个来测试会话是否已经在index.php页面上被销毁:
print_r("session".$_SESSION);
每个页面也有以下代码来防止缓存:
header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header ("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header ("Pragma: no-cache");
<meta http-equiv="expires" content="WED, 01 JUL 2009 05:00:00 GMT" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="pragma" content="no-cache" />
我可以成功地阻止用户输入网址url并使用下面的代码访问安全页面:
if(!isset($_SESSION['user']))
{
echo 'no session in here';
session_start();
session_unset();
session_destroy();
session_write_close();
setcookie(session_name(),'',0,'/');
session_regenerate_id(true);
$_SESSION['username'] = '';
header("Location: index.php");
die("Redirecting to: index.php");
}
会话必须在脚本的第一行开始
<?php session_start();
if(!isset($_SESSION['user']))
{
echo 'no session in here';
session_unset();
session_destroy();
session_write_close();
setcookie(session_name(),'',0,'/');
session_regenerate_id(true);
$_SESSION['username'] = '';
header("Location: index.php");
die("Redirecting to: index.php");
}