Results 1 to 12 of 12

Thread: php and image caching problem

  1. #1

    php and image caching problem

    Hello! You guys have been so helpful in the past.

    I have a website where users build their own human avatar images (like paper dolls) and use them around the message board.

    However there's a problem. When a user updates their avatar image, the server saves it correctly but everyone's browser caches the old one and displays that for a few days :/

    Suggestions?

  2. #2
    use this in your message board pages head section

    <META HTTP-EQUIV="Pragma" CONTENT="no-cache">

    and also paste a notice for users to refresh with ctrl+f5 to see the new avatars themselves.

    btw, it can be from their ISP sides too who uses a cache server to give a false impression of fast net speed to the users...

  3. #3
    You can send these headers too.
    PHP Code:
    <?php
    header
    ("Cache-Control: no-cache, must-revalidate");
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 
    ?>
    Web Design and Web Development blog
    Designer Notice: Browser List for design compatibility

  4. Be careful with those, however. If you run a forum with a lot of traffic and disable caching you run the risk of shooting your bandwidth through the roof. Caching exists for a reason.

    If you MUST control users' cache, do it in a way that you ONLY affect images that have been modified since last cache.
    "Give me the place to stand, and I shall move the earth" - Archimedes
    "The whole problem with the world is that fools and fanatics are always so certain of themselves, but wiser people so full of doubts." - Bertrand Russell

  5. #5
    thanks a lot, thats really helpful. is there a way to direct the no-cache rule to just one specific image?

  6. Are you creating the images dynamically at runtime or are the user-images saved on a server as static files?
    "Give me the place to stand, and I shall move the earth" - Archimedes
    "The whole problem with the world is that fools and fanatics are always so certain of themselves, but wiser people so full of doubts." - Bertrand Russell

  7. #7
    I had the exact same problem and found this thread via google. I just found a nice work around about 5 minutes ago....

    $randy = md5( uniqid (rand(), 1) );

    then make your image like:

    $profile_img = "<img src='$imageLocation2?$randy' border='0'/>";

    echo $profile_img;

    This puts a random string on the end of the jpg file like ?3984289040823 and the page thinks it is a new image and loads it.

  8. #8
    slic- they are saved as static files

    spetsacdc- thank you! that looks like a good solution :]

  9. #9

    Thanks! that solution totally helped me

    Quote Originally Posted by spetsacdc View Post
    I had the exact same problem and found this thread via google. I just found a nice work around about 5 minutes ago....

    $randy = md5( uniqid (rand(), 1) );

    then make your image like:

    $profile_img = "<img src='$imageLocation2?$randy' border='0'/>";

    echo $profile_img;

    This puts a random string on the end of the jpg file like ?3984289040823 and the page thinks it is a new image and loads it.

  10. #10
    I forgot about this thread! Glad it helped!

  11. #11

    Thank you

    Thank you, this thread and code solved my issue.

    I had the same problem, image caching in IE but not Firefox.

    Solution worked perfectly.

  12. #12
    That's the same as invalidating the cache every pageload. You should set the unique key in your database when the image is created and store it along with the image then use the SAME random identifier every time you display the image (until it is updated and then has a new random identifier).

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012