Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

prevent browser back button cache

Tags:

php

caching

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?

like image 624
Ethereal soul Avatar asked Jul 30 '15 22:07

Ethereal soul


People also ask

Which of the following codes can be used to disable caching on back button of the browser?

header("Pragma: no-cache"); // HTTP 1.0.

What is back forward cache in Chrome?

Back/forward cache (or bfcache) is a browser optimization that enables instant back and forward navigation.


1 Answers

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.

like image 109
Ethereal soul Avatar answered Oct 07 '22 14:10

Ethereal soul