Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我怎么能不允许用户在PHP中注销后返回?_Php - Fatal编程技术网

我怎么能不允许用户在PHP中注销后返回?

我怎么能不允许用户在PHP中注销后返回?,php,Php,我刚刚编写了一个PHP登录脚本,我试图实现的是,当用户单击注销链接时,在他们注销后,无论单击浏览器的后退按钮,他们都无法访问该页面 以下是注销功能: //Start the Session session_start(); session_destroy(); header("location:login.php"); exit(); 我确实在所有页面上放置了以下代码,但这似乎不起作用: header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

我刚刚编写了一个PHP登录脚本,我试图实现的是,当用户单击注销链接时,在他们注销后,无论单击浏览器的后退按钮,他们都无法访问该页面

以下是注销功能:

//Start the Session
session_start();
session_destroy();

header("location:login.php");
exit();
我确实在所有页面上放置了以下代码,但这似乎不起作用:

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");

//Start the Session
session_start();

有什么建议吗?

如果没有登录$\u会话,只需重定向即可,例如:

//on your protected pages
session_start();
if(!$_SESSION['logged']) {
    header("location:login.php");
}
这就是我的注销所做的:

session_start();
// Unset all of the session variables.
$_SESSION = array();
// If it's desired to kill the session, also delete the session cookie.
// Note: This will destroy the session, and not just the session data!
if (isset($_COOKIE[session_name()])) {
    setcookie(session_name(), '', time()-42000, '/');
}
// Finally, destroy the session.
session_destroy();

检查用户注销时会话全局是否仍设置为正确的值

print_r($_SESSION);

原因是您正在执行会话销毁,然后执行头重定向,发生的情况是您强制重定向,并且会话的销毁不会以这种方式写入服务器。

您无法控制服务器上客户端后退按钮的工作。您可以在客户端上使用javascript销毁历史数据


客户端可以完全忽略无缓存头。

我建议您将HTTPS与SSL结合使用。您可以关闭SSL会话并将用户踢回非加密页面

大多数浏览器采用不同的缓存方案

例如,在Opera中,您可以单击“上一步”,它将直接从内存中提取页面数据,而不向服务器发送任何数据,即使页面已过期。当然,如果您点击刷新,您的服务器将需要登录


在Internet Explorer中,处理方式非常不同,表单数据会重新提交到服务器。

这可能是您的会话\u destroy()函数。试试这个:

unset($_SESSION);
取消设置$\u会话变量将清除此处存储的任何内容


签出

我认为您需要在会话中存储一些内容,然后在每次加载页面时进行检查。这是我过去的做法

登录脚本(简化) 注销脚本: 每页的顶部
这也有效。


<?
session_start();
if (!isset($_SESSION['username']) && !isset($_SESSION['password'])) {
    header('Location:../index.php');
    exit;
} else {
    session_destroy();
}
?>
这对我很有帮助。。将此粘贴到每页或注销所在的页面

<?php
session_start();
session_unset();
session_destroy();
header("Location:../index.php");
exit;

Olafur,我想我快到了。如果我没有弄错的话,最好将$\u SESSION superglobal设置为nothing,比如:$\u SESSION=array();尽管如此,我仍然可以单击浏览器后退按钮,看到没有动态内容的页面。我想指出,我的注销是一个指向logout.php的简单链接。我应该将注销设置为按钮表单而不是链接吗?请参见,当单击“上一步”浏览器执行此操作时,“上一步”不会重新发送要处理的页面。还有其他建议吗?您应该检查一下,如果用户使用空会话变量访问站点,他应该通过header()重定向被抛出。问题是我没有将superglobal$\u会话['logged']设置为null。因此,使用print\u r($\u会话)进行检查;帮助我找到问题所在。谢谢。然后应该设置一个javascript,在单击注销时将用户发送到logout.php吗?不要忘记javascript不需要在客户端上启用,所以不要依赖它,你是对的。那么,在用户单击“注销”后,如何防止用户访问站点时单击“浏览器后退”按钮呢?Paul,是的,这会将会话变量设置为零。但我遇到的问题是,当我单击浏览器后退按钮时,我仍然可以访问上一个成员页面。但是,显示的成员页面没有来自会话变量的动态内容。我试图做到的是,当单击“注销”时,用户即使单击“浏览器后退”按钮也无法访问该网站。
session_start()

// make sure user is logged in
if (!$_SESSION['username']) {
    $loginError = "You are not logged in.";
    include("index.php");
    exit();
}
$_SESSION['blah'] = '';
<?
session_start();
if (!isset($_SESSION['username']) && !isset($_SESSION['password'])) {
    header('Location:../index.php');
    exit;
} else {
    session_destroy();
}
?>
<?php
session_start();
session_unset();
session_destroy();
header("Location:../index.php");
exit;