How can I prevent caching on my website?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • meekotherogue
    New Member
    • Jun 2010
    • 4

    How can I prevent caching on my website?

    I'm trying to prevent the caching of an image on my website. You see, the image is created dynamically, and when the user navigates throughout the site it changes each time. However, it's when the user tries to go back is the problem. The previous image is displayed instead of the new, dynamically created one. The correct image is created, it's just not being displayed. I have no idea where the previous image comes from, because it's destroyed right after its displayed and the buffer is cleared, so it must be the cache.

    I have included these tags on every page of my website:

    <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
    <META http-equiv="expires" content="-1">
    <META HTTP-EQUIV="Pragma" CONTENT="no-cache">

    The rest of my code is php and a little bit of javascript. The image is rendered using GD.
    Any ideas?
  • zorgi
    Recognized Expert Contributor
    • Mar 2008
    • 431

    #2
    To prevent only image from caching use little trick like this:

    Code:
    <img src="path/to/your/image/img.jpg?nocache=<?php echo time()?>">

    Comment

    • TheServant
      Recognized Expert Top Contributor
      • Feb 2008
      • 1168

      #3
      When you say "dynamicall y created one" are you taking about a captcha?

      Comment

      • meekotherogue
        New Member
        • Jun 2010
        • 4

        #4
        Originally posted by zorgi
        To prevent only image from caching use little trick like this:

        Code:
        <img src="path/to/your/image/img.jpg?nocache=<?php echo time()?>">
        Thanks a lot!! That worked wonderfully!! Could you per chance let me know why/how that works..?

        @TheServant for the record, no I'm not talking about captcha, just an image that is generated based on what the user clicks on.

        Comment

        • zorgi
          Recognized Expert Contributor
          • Mar 2008
          • 431

          #5
          @meekotherogue
          time() gives different result once every second.

          Lets say you have situation where time() gives you 1281651973. That would produce following path:

          Code:
          path/to/your/image/img.jpg?nocache=1281651973
          Very next next second that path looks like this:

          Code:
          path/to/your/image/img.jpg?nocache=1281651974
          Difference is minimal (only last digit) but reason enough for reloading.

          Comment

          • londres9b
            New Member
            • Apr 2010
            • 106

            #6
            Or you could use .htaccess if your server allows it.

            It would be much easier and you wouldn't have to insert that piece of code on every single image.

            Insert this on the .htaccess file:

            Code:
            <FilesMatch ".(jpg|jpeg|png|gif|bmp)$">
            Header set Cache-Control "max-age=0"
            </FilesMatch>

            Comment

            • johny10151981
              Top Contributor
              • Jan 2010
              • 1059

              #7
              It can be done with php too
              header("Cache-Control: max-age=0")

              Comment

              • meekotherogue
                New Member
                • Jun 2010
                • 4

                #8
                @zorgi that makes perfect sense! Thanks for the explanation, I like to know why my code works :P

                @londres9b and @johny10151981 that seems like a good idea too, I appreciate your help :)

                Comment

                Working...