Forum Moderators: coopster

Message Too Old, No Replies

Last-Modified header with genereted content

How do I use the Last-Modified header when generating pages from a database

         

Egil

2:20 pm on Jun 29, 2006 (gmt 0)

10+ Year Member



When you don't use URL rewriting the webpage’s will be served with the different headers that help the browsers cache pages and save bandwidth when re-requesting them.

However, when I use URL rewriting and generated a page from content in a database, that may or may not have been updated since the visitor was last on my page, I loose the ability to tell the user that they already have a current version of a given page in their cache.

How do I regain that option? I assume I need to read the HTTP header from the client and see what version the client has on her computer, and then either do a short http header response saying “nothing changed” or resent the entire page. Am I going in the right direction here?

Best regards, Egil.

eelixduppy

2:25 pm on Jun 29, 2006 (gmt 0)



Welcome to WebmasterWorld!

Many proxies and clients can be forced to disable caching with:


<?php
header [us3.php.net]("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
?>

--Excerpted from [php.net...]

Egil

3:44 pm on Jun 29, 2006 (gmt 0)

10+ Year Member



Hi eelixduppy
I don't want to disable caching on the client, I want to utilize it to save bandwidth.
Let me try to explain myself differently:

  • I want to set the Last Modified header when I send a page in response to a normal HTTP GET (thanks for showing me how).

  • If the clients send a “conditional HTTP GET”, e.g. a HTTP GET that looks something like this:

    GET /somepath/ HTTP/1.1
    Host: www.exampel.com
    If-modified-since: Wed, 2 Jul 2006 09:23:24

    Then I want to check the “If-modified-since” parameter against the “last updated” field in the database for the given page, and if the page has not been modified, I want to respond with a HTTP/1.1 304 Not Modified …, otherwise I’ll return the page of course.

    Has anybody done this before, if so, how did you do it?

  • jatar_k

    8:03 pm on Jun 29, 2006 (gmt 0)

    WebmasterWorld Administrator 10+ Year Member



    you could store the date that generated page was last modified and just output a Last-Modified header every time. The when you edit any given row in your db just change the modified date.

    Egil

    3:23 pm on Jun 30, 2006 (gmt 0)

    10+ Year Member



    hi jatar_k
    That was indeed my plan. The thing I'm trying to figure out is how to read the "If-modified-since" in the conditional get and how to return a HTTP 304 instead of HTTP 200 from php.

    Also, more generally, if anybody else had considered what I'm trying to do, and what their thoughts is about it - is it worth the hassle, extra cpu time etc.

    Thanks for replying, Egil.

    jatar_k

    5:06 pm on Jun 30, 2006 (gmt 0)

    WebmasterWorld Administrator 10+ Year Member



    I don't know if it is worth the hassle, as long as you are setting the proper Last-Modified header the browser/spider should react accordingly.

    eeek

    7:31 pm on Jun 30, 2006 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



    Then I want to check the &#8220;If-modified-since&#8221; parameter against the &#8220;last updated&#8221; field in the database

    Why not just use the last updated field in the database to set the http Last-Modified: header?

    Egil

    12:14 am on Jul 1, 2006 (gmt 0)

    10+ Year Member



    My plan IS to set the Last-Modified header field when ever I return a page.

    But if a client does a "Conditional GET" (look a few posts back for a definition of a Conditional GET) I want to responde with HTTP 304 if the client already has the most up to date version of the page.