SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Mar 2001
    Location
    North Wales, UK
    Posts
    181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Writing cache friendly scripts - IE problems

    In an effort to reduce traffic to one of my sites, I've recently attempted to make the pages cache friendly by sending the appropriate last-modified or 304 headers.

    Here's the code:

    PHP Code:
    $browserLastModified = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) : 0;
    if (
    $this->lastModified <= $browserLastModified)
    {
        
    header('HTTP/1.1 304 Not modified');
        exit();
    }
    else
    {
        
    header('Last-Modified: '.gmdate('r',$this->lastModified));
        
    header("Cache-Control: no-store, no-cache, must-revalidate");  // HTTP/1.1
        
    header("Cache-Control: post-check=0, pre-check=0"false);
        
    header("Pragma: no-cache"); // HTTP/1.0

    $this->lastModified is a UNIX timestamp of the last modification time of the current page.

    I'd thought this was working fine, but IE 6 seems to be ignoring the headers and often serving a cached version of the page without checking with the server.

    For example, a page of comments. In IE 6, if I view the comments page, then click the 'add comment' link, post a comment, and then click a link back to the comments page; IE 6 serves a cached version of the comments page which doesn't include my new comment. So it's ignoring the headers and not checking back with the server to see if the page has changed. In Firebird (my browser of choice), it works fine. Unfortunately ~95% of my users use IE.

    Does anyone have any ideas how to get IE to behave? The only idea I have at the moment is to put a random query string in the link back to the comments page, so the browser thinks it's a completely different page and therefore refetches it. But I'd rather avoid that if possible.

  2. #2
    public static void brain Gybbyl's Avatar
    Join Date
    Jun 2002
    Location
    Montana, USA
    Posts
    647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If the link you are clicking to get back to the comments page is 'javascript:history.go(-1)', then I believe it heeds no cache at all, and just shows whatever was actually on the page when you viewed it last. You should put an actual hard link back to the page if you want it to refresh properly. At least this is what I find happens often times (i've had trouble with this same problem very recently). It sounds like this might not even be the problem, I'm just throwing out one of the solutions that I've tried.

    You might try just sending the no-cache headers everytime, instead of trying to battle with caching sometimes and not caching other times.

    *off topic*
    I think someone needs to make an IE skin for Firebird, so that I can install it on all of my friends' computers -- They will never know the difference (except that it will be a whole lot more sexy)
    Ryan

  3. #3
    SitePoint Zealot
    Join Date
    Mar 2001
    Location
    North Wales, UK
    Posts
    181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's a proper absolute (/comments/view.php?id=blah) link back to the page, so it's not that.

    Just tried sending the no-cache headers all the time but that didn't make any difference.


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
  •