【问题标题】:PHP - Weird page refresh issuePHP - 奇怪的页面刷新问题
【发布时间】:2015-04-28 08:55:56
【问题描述】:

我的 php 网站上的页面需要一直刷新。例如,我为每个不同的用户设置了一个时间表,如果我登录并查看时间表并注销,那么当我以其他用户身份登录并查看该时间表时,它将显示以前的人员时间表,除非我刷新它。我的网站上有更多页面存在此问题。我需要在注销时做一些额外的事情吗?我知道我可以使用 ctrl+f5 但我希望网站能够为我管理事情。有没有其他人对此有类似的问题任何建议?

这是我的注销代码:

<?php
//include 'header.php';
session_start();
include 'dbconnect.php';
//set the session date in to an empty array
$_SESSION = array();
//Expire thier cookie files
if (isset($_COOKIE["user"]) && isset($_COOKIE["pass"]))
{
    setcookie("user", '', strtotime( '-10 days'), '/');
    setcookie("pass", '', strtotime( '-10 days'), '/');
    session_destroy();

}
//destroy the session variables
session_destroy();
//double check if the user exists
if (isset($_SESSION['username']))
{
    header("Location: message.php?msg=Error:_Logout_Failed");
} else {
    session_destroy();

    header("Location: index.php");
    exit(); 
}
session_destroy();

?>

【问题讨论】:

    标签: php browser-cache


    【解决方案1】:

    首先,session_destroy() 本身不足以破坏会话数据,请参阅How can I clear my php session data correctly?

    发件人:https://stackoverflow.com/a/6472150/3536236

    使用session_destroy() 后,会话cookie 被删除,并且 session 不再存储在服务器上。 $_SESSION 中的值可能 仍然可用,但它们不会出现在下一个页面加载中。

    编辑:通常要确保清除会话数据,请尝试以下操作:

    session_start();
    $_SESSION = array(); //clears the session data. 
    session_destroy();
    

    其次,这很可能只是因为您在同一台机器上以多个用户的身份登录,而这可能不会发生在非管理员用户/主机上。您显示的代码中没有任何内容实际显示数据的显示方式,如果您正在显示来自诸如文件(而不是数据库资产)等固定点的数据,您还可以探索 可能性 clearstatcache() 可以帮助你。

    在你的 PHP 输出页面上设置 headers 来强制页面刷新,服务器和 header 信息可能允许浏览器缓存页面,所以添加类似

    header("Cache-Control: no-cache, must-revalidate"); //HTTP 1.1
    header("Expires: Sat, 26 Jul 2007 05:00:00 GMT"); // Date in the past 
    

    到您的输出页面(HTML 代码上方),以确保浏览器不会缓存它们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-17
      • 2020-08-16
      • 1970-01-01
      • 1970-01-01
      • 2021-12-31
      • 2012-08-02
      相关资源
      最近更新 更多