Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
230 views
in Technique[技术] by (71.8m points)

php - prevent browser back button cache

here is the code i have written

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>

and 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>

and the problem is this should have prevent the cache in the browser through header() function but after loading the page the back button is taking me to the previous page and the forward button of the browser is taking me to the next page can any one help how to prevent this browser cache or why is my code here is not working?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Thanks guys for helping me out but i found a way that force the browser to prevent cache and i have used the below code :

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

here is the reference : Making sure a web page is not cached, across all browsers and its working pretty good.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...