prevent caching of gd image

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dave Cheseldine

    prevent caching of gd image

    Hi
    I want to stop a gd generated image from being cached.

    It is called into index.php file as follows:

    <image src =
    "http://www.daveches.co .uk/importimage/importimage.php ?x=$randomNumbe r"
    alt = "battle area" usemap="#battle grid" border="0">

    (importimage4re al.php contains the gd functionality that load and modify
    the image).

    The random number querystring in the code above is an attempt to trick
    the browser into thinking the image is different from what it has in
    its cache. But still it displays its cached version of the image.

    I have tried every "stop caching" header I can think of in both
    index.php and importimage.php , set expires to a time in the past and all
    the rest. Still, refresh the browser: you get the cached, not the
    latest, image (same in ie6 and Firefox). After a few minutes something
    expires and you do get the latest version by Refreshing, but this is no
    good for my application.

    Is it possible that some settings on my hosting company's server are
    overwriting any changes I can make?

    dave Cheseldine
  • WhatsPHP

    #2
    Re: prevent caching of gd image

    It might not be just the PHP caching.. Did u check the IE and firefox
    browser setting? in IE: Tools > Internet Options > Settings check if
    the setting shows you how the caching is done.. this might help.

    Comment

    • AD7six

      #3
      Re: prevent caching of gd image

      Try this in your import image script.

      //**** Lots of headers to ensure the image is not cached.
      // 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");
      //**** End of crappy headers.

      It's not my own work, and I don't remember where I got it from but it
      worked for me for a similar application.

      Regards,

      AD7six

      Comment

      • dave ches

        #4
        Re: prevent caching of gd image

        Hi
        Thanks: yes I've tried those headers. It still delivers a cached
        version of the gd image unless the browser is idle for a minute or 2.

        I'm going to contact my hosting provider and see if it's anything in the
        server config.

        What I don't understand is how it can go to the cached version of the
        php script that generates the gd image even when the php script is
        called via a URL with a unique querystring. (and it is: each time you
        refresh and look at the source it is a different querystring.

        regards
        dave



        AD7six wrote:[color=blue]
        > Try this in your import image script.
        >
        > //**** Lots of headers to ensure the image is not cached.
        > // 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");
        > //**** End of crappy headers.
        >
        > It's not my own work, and I don't remember where I got it from but it
        > worked for me for a similar application.
        >
        > Regards,
        >
        > AD7six
        >[/color]

        Comment

        • Philip Ronan

          #5
          Re: prevent caching of gd image

          "dave ches" wrote:
          [color=blue]
          > Thanks: yes I've tried those headers. It still delivers a cached
          > version of the gd image unless the browser is idle for a minute or 2.
          >
          > I'm going to contact my hosting provider and see if it's anything in the
          > server config.
          >
          > What I don't understand is how it can go to the cached version of the
          > php script that generates the gd image even when the php script is
          > called via a URL with a unique querystring. (and it is: each time you
          > refresh and look at the source it is a different querystring.[/color]

          This sounds more like a browser problem than anything else. Can we have some
          URLs please (i.e., the image script and the page that references it)? The
          URL you posted earlier just points to a 404 error page.

          --
          phil [dot] ronan @ virgin [dot] net


          Comment

          • AD7six

            #6
            Re: prevent caching of gd image

            dave ches,

            URL would help investigations. I'm interested to here what the problem
            is, when it is found.

            What does a site like this http://www.delorie.com/web/headers.html have
            to say about your headers?

            Regards,

            AD7six

            Comment

            • dave ches

              #7
              Re: prevent caching of gd image

              Hi
              Thanks for the interest.

              delorie reports as follows:
              for the file that references the image:

              HTTP/1.1 200 OK
              Date: Wed, 23 Nov 2005 14:59:42 GMT
              Server: Apache/2.0.54 (Unix) mod_perl/1.99_17 Perl/v5.8.7
              FrontPage/5.0.2.2635 PHP/4.3.11 mod_webapp/1.2.0-dev mod_python/3.1.4
              Python/2.2.2 mod_ssl/2.0.54 OpenSSL/0.9.7d
              X-Powered-By: PHP/4.4.1
              Expires: Mon, 26 Jul 1997 05:00:00 GMT
              Cache-Control: no-store, no-cache, must-revalidate, post-check=0,
              pre-check=0
              Pragma: no-cache
              Last-Modified: Wed, 23 Nov 2005 14:59:42 GMT
              Vary: Accept-Encoding
              Connection: close
              Content-Type: text/html; charset=iso-8859-1


              for the file containing the image script:

              HTTP/1.1 200 OK
              Date: Wed, 23 Nov 2005 15:01:24 GMT
              Server: Apache/2.0.54 (Unix) mod_perl/1.99_17 Perl/v5.8.7
              FrontPage/5.0.2.2635 PHP/4.3.11 mod_webapp/1.2.0-dev mod_python/3.1.4
              Python/2.2.2 mod_ssl/2.0.54 OpenSSL/0.9.7d
              X-Powered-By: PHP/4.4.1
              Expires: Mon, 26 Jul 1997 05:00:00 GMT
              Cache-Control: no-store, no-cache, must-revalidate, post-check=0,
              pre-check=0
              Pragma: no-cache
              Last-Modified: Wed, 23 Nov 2005 15:01:24 GMT
              Connection: close
              Content-Type: image/gif



              AD7six wrote:[color=blue]
              > dave ches,
              >
              > URL would help investigations. I'm interested to here what the problem
              > is, when it is found.
              >
              > What does a site like this http://www.delorie.com/web/headers.html have
              > to say about your headers?
              >
              > Regards,
              >
              > AD7six
              >[/color]

              Comment

              • AD7six

                #8
                Re: prevent caching of gd image

                test URL please.

                It's only the headers for the image file that are relavent, all _seems_
                present and correct. Have you deleted your cache before testing?

                Regards,

                AD7six

                Comment

                • dave ches

                  #9
                  Re: prevent caching of gd image

                  Hi
                  The URL is www.companybattleships.com/index.php

                  The best way to show the unwanted caching is as follows.

                  1. open 2 new browsers.

                  2. in browser 1, go to www.companybattleships.com/index.php

                  3. in browser 2, go to www.companybattleships.com/callingpage.php

                  4. follow the link in browser 2. Observe that the image has changed.

                  5. refresh browser 1. The image has also changed. Great.

                  6. in browser 2, go back to www.companybattleships.com/callingpage.php

                  7. follow the link in browser 2 once again. Observe that the image has
                  changed again.

                  8. refresh browser 1. The image does not change. Problem.

                  cheers
                  dave

                  AD7six wrote:[color=blue]
                  > test URL please.
                  >
                  > It's only the headers for the image file that are relavent, all _seems_
                  > present and correct. Have you deleted your cache before testing?
                  >
                  > Regards,
                  >
                  > AD7six
                  >[/color]

                  Comment

                  • AD7six

                    #10
                    Re: prevent caching of gd image

                    Well that's not an easy one, I can't tell if it's updating or not (on
                    any step) ;o).

                    Are you sure the image is being updated on every request? Compare the
                    behaviour of the below link when visiting multiple times & refreshing.
                    I think your headers are probably fine and the image is not being
                    updated.



                    Regards,

                    AD7six

                    Comment

                    • dave ches

                      #11
                      Re: prevent caching of gd image

                      Yes, the image is being updated every time you follow the link on



                      you can see that because 6 additional black squares appear on the image
                      each time you follow that link.

                      But in your other browser (the one where you arrive at index.php with
                      not referer) you don't see the latest image: compare the 2 and the
                      black squares aren't there.

                      So why is the latest image not being displayed if the headers are in order?

                      In the example you send, the 2 images:
                      This image does not set any extra HTTP Headers
                      and
                      This image has headers set to prevent caching

                      are identical, so doesn't that just confirm that the headers don't do as
                      they are supposed to when drawing in gd images?

                      regards
                      dave

                      AD7six wrote:[color=blue]
                      > Well that's not an easy one, I can't tell if it's updating or not (on
                      > any step) ;o).
                      >
                      > Are you sure the image is being updated on every request? Compare the
                      > behaviour of the below link when visiting multiple times & refreshing.
                      > I think your headers are probably fine and the image is not being
                      > updated.
                      >
                      > http://www.noswad.me.uk/cachetest.html
                      >
                      > Regards,
                      >
                      > AD7six
                      >[/color]

                      Comment

                      Working...