Page still caches even with header() and random query string - HELP

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phil Powell

    Page still caches even with header() and random query string - HELP

    I created a page that will be doing image resizing and manipulation,
    which seems to work (using GD library). However, upon returning to
    the page where the image has been changed, I still see the old image,
    until I refresh my browser and then zappo, there it is! All changed!

    I have done everything I can think of to force caching including this:

    [PHP]
    // Date in the past
    header("Expires : Mon, 26 Jul 1997 05:00:00 GMT");

    // always modified
    header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");

    // HTTP/1.1
    header("Cache-Control: no-store, no-cache, must-revalidate");
    header("Cache-Control: post-check=0, pre-check=0", false);

    // HTTP/1.0
    header("Pragma: no-cache");
    [/PHP]

    And even including a "junk" query string value that returns a random
    string of characters:

    Code:
    http://.../..index.php?id=$id&section=$section&action=$action&junk=<?=
    randString(16); ?>
    All to no avail!! The page STILL caches no matter what I do, only to
    show the changed image if I refresh my browser.

    The URL will always be "index.php" for ease of architectural build
    (using query string values to deliver the right classes and methods to
    perform whatever action is necessary). Again, everything works, just
    can't view my changed image unless I manually refresh my browser.

    Any other ideas?

    Phil
  • Pedro Graca

    #2
    Re: Page still caches even with header() and random query string - HELP

    [ Followup-To: comp.lang.php ]

    Phil Powell wrote:[color=blue]
    > I created a page that will be doing image resizing and manipulation,
    > which seems to work (using GD library). However, upon returning to
    > the page where the image has been changed, I still see the old image,
    > until I refresh my browser and then zappo, there it is! All changed!
    >
    > I have done everything I can think of to force caching including this:[/color]
    (snip)[color=blue]
    > Any other ideas?[/color]

    Are you putting the cache-control stuff /within/ the image generation
    script?

    Something like:

    <?php
    // create the image
    $image = imagecreate();
    // ...

    // prevent caching for *this* image
    header('Expires : ...');
    header('Last-Modified: ...');
    header('Cache-Control: ...');
    header('Pragma: ...');

    // send the image to the browser
    header('Content-Type: image/png');
    imagepng($image );
    imagedestroy($i mage);
    ?>
    --
    --= my mail box only accepts =--
    --= Content-Type: text/plain =--
    --= Size below 10001 bytes =--

    Comment

    • Justin Koivisto

      #3
      Re: Page still caches even with header() and random query string- HELP

      Pedro Graca wrote:[color=blue]
      > [ Followup-To: comp.lang.php ]
      >
      > Phil Powell wrote:
      >[color=green]
      >>I created a page that will be doing image resizing and manipulation,
      >>which seems to work (using GD library). However, upon returning to
      >>the page where the image has been changed, I still see the old image,
      >>until I refresh my browser and then zappo, there it is! All changed!
      >>
      >>I have done everything I can think of to force caching including this:[/color]
      >
      > (snip)
      >[color=green]
      >>Any other ideas?[/color]
      >
      > Are you putting the cache-control stuff /within/ the image generation
      > script?[/color]

      Usually, I don't even bother with that because the random query string
      usually does the trick for me. Of course, I tend to put the random part
      at the beginning of the URI like:

      <img src="img_gen.ph p?j=fjgs749Dj4F otFDJ&id=<?php echo $id
      ?>&section=<?ph p echo $section ?>&action=<?ph p echo $action ?>"
      alt="Generated Image">

      --
      Justin Koivisto - spam@koivi.com
      PHP POSTERS: Please use comp.lang.php for PHP related questions,
      alt.php* groups are not recommended.
      Official Google SERPs SEO Competition: http://www.koivi.com/serps.php

      Comment

      • Pedro Graca

        #4
        Re: Page still caches even with header() and random query string - HELP

        This script works for me
        browser is MozillaFirebird / Win2K;
        server is PHP 4.3.3 / Apache 1.3.29



        #v+
        <?php
        header('Expires : 0');
        header('Cache-Control: no-cache, no-store, private');
        header('Pragma: no-cache');

        $d = date('Ymd');
        $h = date('H:i:s');
        list($u, $dummy) = explode(' ', microtime());
        $u = substr($u . '000000', 0, -8);

        if (isset($_GET['img'])) {

        $im = imagecreate(90, 60);
        $r = rand(0, 255); $g = rand(0, 255); $b = rand(0, 255);
        $bgcolor = imagecoloralloc ate($im, $r, $g, $b);
        $black = imagecoloralloc ate($im, 255-$r, 255-$g, 255-$b);
        imagestring($im , 5, 5, 2, $d, $black);
        imagestring($im , 5, 5, 22, $h, $black);
        imagestring($im , 5, 5, 42, $u, $black);
        header('Content-Type: image/png');
        imagepng($im);
        imagedestroy($i m);
        exit();

        }

        echo <<<HTML
        <html>
        <head>
        <title>image test</title>
        <meta http-equiv="Pragma" content="no-cache">
        <meta http-equiv="Expires" content="0">
        </head>
        <body>
        <img src="imgtest.ph p?img&u=$u" alt="date/time as image"/><br/>
        <a href="">refresh </a> @ $d $h $u<br/>
        See just the <a href="imgtest.p hp?img&u=$u">im age</a> and then hit the
        &quot;Back&quot ; button.
        </body></html>
        HTML;
        ?>
        #v-


        Hope you can make it work for you too :)
        --
        --= my mail box only accepts =--
        --= Content-Type: text/plain =--
        --= Size below 10001 bytes =--

        Comment

        • Phil Powell

          #5
          Re: Page still caches even with header() and random query string - HELP

          Actually using the header('Content-type: image/png'); type command caused my
          entire index.php to be converted to a PNG image and caused severe HTTP
          errors to generate.

          Nothing worked.. until I tried something very very simple:

          <img src=blah.jpg?ju nk=<?= randString(16) ?>>

          That did it!!

          Phil

          "Pedro Graca" <hexkid@hotpop. com> wrote in message
          news:bvmp04$tkk iu$1@ID-203069.news.uni-berlin.de...[color=blue]
          > This script works for me
          > browser is MozillaFirebird / Win2K;
          > server is PHP 4.3.3 / Apache 1.3.29
          >
          >
          >
          > #v+
          > <?php
          > header('Expires : 0');
          > header('Cache-Control: no-cache, no-store, private');
          > header('Pragma: no-cache');
          >
          > $d = date('Ymd');
          > $h = date('H:i:s');
          > list($u, $dummy) = explode(' ', microtime());
          > $u = substr($u . '000000', 0, -8);
          >
          > if (isset($_GET['img'])) {
          >
          > $im = imagecreate(90, 60);
          > $r = rand(0, 255); $g = rand(0, 255); $b = rand(0, 255);
          > $bgcolor = imagecoloralloc ate($im, $r, $g, $b);
          > $black = imagecoloralloc ate($im, 255-$r, 255-$g, 255-$b);
          > imagestring($im , 5, 5, 2, $d, $black);
          > imagestring($im , 5, 5, 22, $h, $black);
          > imagestring($im , 5, 5, 42, $u, $black);
          > header('Content-Type: image/png');
          > imagepng($im);
          > imagedestroy($i m);
          > exit();
          >
          > }
          >
          > echo <<<HTML
          > <html>
          > <head>
          > <title>image test</title>
          > <meta http-equiv="Pragma" content="no-cache">
          > <meta http-equiv="Expires" content="0">
          > </head>
          > <body>
          > <img src="imgtest.ph p?img&u=$u" alt="date/time as image"/><br/>
          > <a href="">refresh </a> @ $d $h $u<br/>
          > See just the <a href="imgtest.p hp?img&u=$u">im age</a> and then hit the
          > &quot;Back&quot ; button.
          > </body></html>
          > HTML;
          > ?>
          > #v-
          >
          >
          > Hope you can make it work for you too :)
          > --
          > --= my mail box only accepts =--
          > --= Content-Type: text/plain =--
          > --= Size below 10001 bytes =--[/color]


          Comment

          • John Dunlop

            #6
            Re: Page still caches even with header() and random query string - HELP

            Phil Powell wrote upsidedown:
            [color=blue]
            > Actually using the header('Content-type: image/png'); type command caused my
            > entire index.php to be converted to a PNG image[/color]

            More likely it caused your browser to treat the data as a PNG image.
            No conversion took place.
            [color=blue]
            > and caused severe HTTP errors to generate.[/color]

            Which were?

            [Concerning non-caching of images:]
            [color=blue]
            > Nothing worked.. until I tried something very very simple:
            >
            > <img src=blah.jpg?ju nk=<?= randString(16) ?>>[/color]

            (That'll result in invalid markup, in every version of HTML to date.)
            [color=blue]
            > That did it!![/color]

            That suggestion is common, but is definitely no substitute for the
            proper method, which has already been suggested: send the appropriate
            headers, for *all* files.

            Rather than going down the road of dynamic URLs, I reckon you'd be
            better off appending a static query string to the image's URL (or just
            a "?"); a dynamic, "once-only" URL (containing a query string) will
            only needlessly clog up cache proxy servers that store URLs with query
            components. And there are some of those, I believe. If you want to
            defeat caches using unique URLs, you ought to ensure that the response
            isn't cacheable. What's the point in having multiple copies of the
            same image stored in caches?

            The reason for appending a query string at all is that "caches MUST
            NOT treat responses to [URLs with "?" in the path] as fresh unless the
            server provides an explicit expiration time" (RFC2616, sec. 13.9). So
            some systems check for the presence of a "?" and, if found, consider
            the response uncacheable. Those cache proxy servers would treat the
            response as stale and request a new copy from the origin server. That
            said, I stand by my assertion that you ought to send appropriate
            headers for every file, whether you use the query trick or not.

            Without appropriate cache headers sent with the HTML file, dynamic
            image URLs are pointless. A browser would fetch the HTML file from a
            cache somewhere along the lines, and retrieve the image using the same
            old URL -- probably from a cache too, if the image's response headers
            contained the relevant caching instructions. That's why it's
            important to send appropriate headers.

            Trial and error techniques aren't very helpful; it's better to
            understand the goings-on. M. Nottingham's "Caching Tutorial for Web
            Authors and Webmasters" is the much-publicised article on the subject:

            http://www.mnot.net/cache_docs/

            The following is an excerpt from that document, in response to the FAQ
            "My images expire a month from now, but I need to change them in the
            caches now!":

            The most effective solution is to rename the files; that way, they
            will be completely new objects, and loaded fresh from the origin
            server. Remember that the page that refers to an object will be
            cached as well. Because of this, it's best to make static images
            and similar objects very cacheable, while keeping the HTML pages
            that refer to them on a tight leash.

            The HTTP-EQUIV nonsense, as usual, isn't worthy of consideration.

            --
            Jock

            Comment

            Working...