SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Zealot xiaawan's Avatar
    Join Date
    Sep 2007
    Location
    Islamabad, Pakistan
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Browser Cache Problem

    Hi All,
    Is there any way to prevent browser not to maintain cache. Any function in PHP or Javascript? Anything we can code to handle cache issue? Please help. Thanks in advance.
    Zia Awan

  2. #2
    SitePoint Addict Jasper Bekkers's Avatar
    Join Date
    May 2007
    Location
    The Netherlands
    Posts
    282
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just send the correct HTTP caching headers. Send something like
    Code:
    cache-control: no-cache
    pragma: no-cache
    expires: -1
    Optionally you could also append something like a timestamp to the querystring to pretend that the content is unique every time.
    Design patterns: trying to do Smalltalk in Java.
    I blog too, you know.

  3. #3
    SitePoint Zealot xiaawan's Avatar
    Join Date
    Sep 2007
    Location
    Islamabad, Pakistan
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear thank you for your quick reply. I have used following in my header file that loads each time a page is loaded.

    Code PHP:
    <?
    	  Header('Cache-Control: no-cache');
    	  Header('Pragma: no-cache');
      ?>

    But it still does not solve my problem. Actually the problem is in IE7 when user clicks the back button (I mean browser back button) and all the previous values can be viewed by the user. Actually it happens when user clicks back button after sign out, and in this case user shouldn't be allowed to view previous pages which are required them to be signed in. Any idea.
    Zia Awan

  4. #4
    SitePoint Enthusiast NoDude's Avatar
    Join Date
    Sep 2007
    Location
    Sofia, Bulgaria
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do a header("Location: url) redirect after logging them out, thatshould solve your problem.

  5. #5
    SitePoint Zealot xiaawan's Avatar
    Join Date
    Sep 2007
    Location
    Islamabad, Pakistan
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I already use header("location: homepage"); but it still does not solve my issue. Actually when I click Browser's back button the page does not get refreshed and all the contents are displayed from cache. And user is able to see previous pages. Any suggestion?
    Zia Awan

  6. #6
    SitePoint Wizard
    Join Date
    Feb 2007
    Posts
    1,274
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your problem is that IE actually does as the http protocol defines: when using the back it displays the original page without requesting it again, regardless of the cache settings. This is one of the few cases where IE actually got it right, FF et al got it wrong.

    Luckily there's a trick that you can use to tell IE to re-request from the server: Set a header with Expires: -1.

  7. #7
    SitePoint Zealot xiaawan's Avatar
    Join Date
    Sep 2007
    Location
    Islamabad, Pakistan
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear thank you for your reply. But it still does not solve my problem. I use this code in my file that loads in every page of the site and here is the code that I use.

    Code HTML4Strict:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/strict.dtd">		 
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>   
    <title>My Site</title>
      <?
    	  Header('Cache-Control: no-cache');
    	  Header('Pragma: no-cache');
    	  Header('Expires: -1');
     
      ?>
    	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

    Am I using this code at the right place?
    Zia Awan

  8. #8
    SitePoint Addict
    Join Date
    Apr 2004
    Location
    Melbourne
    Posts
    362
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does "Cache-Control: private" help at all?

  9. #9
    SitePoint Zealot xiaawan's Avatar
    Join Date
    Sep 2007
    Location
    Islamabad, Pakistan
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now I use the followings but it still does not solve my problem

    Code PHP:
      <?
    	  Header('Cache-Control: no-cache');
    	  Header('Cache-Control: private');
    	  Header('Pragma: no-cache');
    	  Header('Expires: -1');
     
      ?>
    Zia Awan

  10. #10
    SitePoint Addict Jasper Bekkers's Avatar
    Join Date
    May 2007
    Location
    The Netherlands
    Posts
    282
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by xiaawan View Post
    Am I using this code at the right place?
    Only if you have a output buffer running, otherwise the header calls should be placed before any output is made.
    Design patterns: trying to do Smalltalk in Java.
    I blog too, you know.

  11. #11
    SitePoint Zealot xiaawan's Avatar
    Join Date
    Sep 2007
    Location
    Islamabad, Pakistan
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear thank you for your reply. This problem is hammering my mind and I am still not getting out of it. I even tried the followings:
    Code HTML4Strict:
      <?
    	  Header('Cache-Control: no-cache');
    	  Header('Cache-Control: private');
    	  Header('Pragma: no-cache');
    	  Header('Expires: -1');
     
      ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/strict.dtd">		 
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>

    But nothing works for me.
    Zia Awan

  12. #12
    SitePoint Enthusiast NoDude's Avatar
    Join Date
    Sep 2007
    Location
    Sofia, Bulgaria
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm curious. What happens if you redirect them to the page that's not supposed to be cached. They won't see the cached results, because their validation will fail, but will that update the back button cache as well?

  13. #13
    SitePoint Zealot xiaawan's Avatar
    Join Date
    Sep 2007
    Location
    Islamabad, Pakistan
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by NoDude View Post
    I'm curious. What happens if you redirect them to the page that's not supposed to be cached. They won't see the cached results, because their validation will fail, but will that update the back button cache as well?
    Let me explain what happens and what I want to be happened.
    I have been developing a web site that has many pages and many of them are required user to be signed in. If a user is viewing his records on a page that requires him to be signed in, and he clicks on sign out link, now the web site should destroy his session and he no longer should have access to any page that requires him to be signed in. Now the problem is after signing out if user clicks browser's back button he is still able to see previous records which he shouldn't be.

    What I am doing here is , when user clicks on sign out, Sessions and Cookies are destroyed and he goes to the home page. After destroying Sessions and Cookies I redirect user to the home page that does not require him to be signed in.
    What I have observed here is that the page does not get refreshed, if the page is refreshed then user is redirected to sign in page. But it's not doing so.
    Any idea that can help me?
    Zia Awan

  14. #14
    SitePoint Enthusiast NoDude's Avatar
    Join Date
    Sep 2007
    Location
    Sofia, Bulgaria
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What I meant was, what would happen if you do a header('Location: '.$_SERVER['HTTP_REFERRER']); after you log the person out. Maybe that would clear the cache from the previous url, because the url the user is redirected matches the one the browser has for the back button, but the content has obviously changed (because you'll be displaying a login form).

  15. #15
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Location
    good ol' blighty
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try the following. it works for me.
    PHP Code:
                header('Expires: Tue, 25 Mar 2003 05:00:00 GMT');
                
    header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
                
    header('Cache-Control: no-cache, must-revalidate');
                
    header('Pragma: no-cache'); 


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •