【问题标题】:prevent browser back button cache防止浏览器后退按钮缓存
【发布时间】:2015-10-22 11:58:11
【问题描述】:

这是我写的代码

index.php:

  <?php session_start(); 
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-cache");
header("Pragma: no-cache");
?>
<!DOCTYPE html>
<html>
<head>
    <title>Student-login</title>
    <link href='http://fonts.googleapis.com/css?family=Shadows+Into+Light' rel='stylesheet' type='text/css'>
    <link rel="stylesheet" type="text/css" href="css/main.css">
    <link rel="stylesheet" type="text/css" href="css/font-awesome.min.css">
    <link rel="shortcut icon" href="css/favicon.jpg"/>
    <link href='http://fonts.googleapis.com/css?family=Questrial' rel='stylesheet' type='text/css'>
</head>
<body>
    <div class="bodybg"></div>
    <div class="main-form">
        <h1>Student Login</h1>
        <form class="login" action="database.php" method="post">
            <input type="text" placeholder="&#xf007; User name(*)" name="username" required></input><br>
            <input type="password" placeholder="&#xf023; Password(*)" name="password" required>
        </input><br>
        <input type="submit" value="Login" name="btnlogin"></input><br>
        <div class="button"><a href="login.php">Register</a></div>

        <!--flash message-->
        <div id="message">
            <?php 
            if(isset($_SESSION['success'])){
                echo $_SESSION['success'];
                //echo "<p class='message'>hello this world!!</p>";

            }else{
                echo " ";
            }
            $_SESSION['success']=' ';
            ?>
        </div>
    </form>
</div>
<div id="heading">
    <p id="p1">American Internation University-Bangladesh</p>
    <p id="p2">Thesis Compilation</p>
</div>
<div id="content">
    <div id="prev"><i class="fa fa-chevron-left fa-1x"></i></div>
    <div id="next" d><i class="fa fa-chevron-right fa-1x"></i></div>
    <div id="pager"></div>
    <div id="slider">
        <div class="item">
            <img src="img/1.jpg">
            <div class="info">
                <h2>Picture 1</h2>
                <p>This is the library</p>
            </div>
        </div>
        <div class="item">
            <img src="img/2.jpg">
            <div class="info">
                <h2>Picture 2</h2>
                <p>This is the Book</p>
            </div>
        </div>
        <div class="item">
            <img src="img/3.jpg">
            <div class="info">
                <h2>Picture 3</h2>
                <p>This is the Pen</p>
            </div>
        </div>
        <div class="item">
            <img src="img/4.jpg">
            <div class="info">
                <h2>Picture 4</h2>
                <p>This is the literature</p>
            </div>
        </div>
        <div class="item">
            <img src="img/5.jpg">
            <div class="info">
                <h2>Picture 5</h2>
                <p>This is the Research</p>
            </div>
        </div>
    </div>  
</div>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/cycle.js"></script>
<script type="text/javascript" src="js/main.js"></script>
</body>
</html>

和home.php:

 <?php 
session_start(); 
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-cache");
header("Pragma: no-cache");
?>
<!DOCTYPE html>
<html>
<head>
    <title>Home</title>
    <link href='http://fonts.googleapis.com/css?family=Shadows+Into+Light' rel='stylesheet'
    type='text/css'>
    <link rel="stylesheet" type="text/css" href="css/main.css">
    <link rel="stylesheet" type="text/css" href="css/font-awesome.min.css">
    <link rel="shortcut icon" href="css/favicon.jpg"/>
</head>
<body class="body"> 

    <div class="menu">
        <div class="home">
            <a href="Home.html">Home</a>
        </div>
        <div class="profile">
            <a href="profile.html">Profile</a>
        </div>
        <div class="contact">
            <a href="contact.html">Contact</a>
        </div>
        <div class="logout">
            <a href="database.php?laction">Logout</a>
        </div>
    </div>
    <div class="transparent">
    </div>
    <div class="content">
        <div id="message">
            <?php 
            if(isset($_SESSION['success'])){
                echo $_SESSION['success'];
                //echo "<p class='message'>hello this world!!</p>";

            }else{
                echo " ";
            }
            $_SESSION['success']=' ';
            ?>
        </div>
    </div>
    <script type="text/javascript" src="js/jquery.js"></script>
    <script type="text/javascript" src="js/main.js"></script>
</body>
</html>

问题是这应该通过 header() 函数阻止浏览器中的缓存,但是在加载页面后,后退按钮将我带到上一页,浏览器的前进按钮将我带到下一页任何人都可以帮助如何防止此浏览器缓存或为什么我的代码在这里不起作用?

【问题讨论】:

  • 缓存选项不控制前进/后退按钮,它们只影响用户导航到 URL 时发生的情况。
  • 没明白你的意思..你能详细描述一下吗?因为即使我导航到上一个网址它也显示了页面,但它不应该这样做吗?@Barmar
  • 后退和前进按钮只是重新显示已经呈现的页面,它们不会重新加载页面。它们不使用网络缓存,因此禁用缓存的命令对它们没有影响。
  • @Etherealsoul,看看这个:github.com/browserstate/history.js

标签: php caching


【解决方案1】:

感谢大家帮助我,但我找到了一种强制浏览器阻止缓存的方法,我使用了以下代码:

header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP 1.1.
header("Pragma: no-cache"); // HTTP 1.0.
header("Expires: 0 "); // Proxies.

这里是参考: Making sure a web page is not cached, across all browsers 而且效果很好。

【讨论】:

    猜你喜欢
    • 2013-01-11
    • 2011-10-15
    • 1970-01-01
    • 1970-01-01
    • 2017-04-23
    • 1970-01-01
    • 2015-09-28
    • 1970-01-01
    • 2013-03-02
    相关资源
    最近更新 更多