Skip to content

Instantly share code, notes, and snippets.

@SabrinaMarkon
Created December 22, 2016 12:04
Show Gist options
  • Save SabrinaMarkon/0da6cf5304acb90f304d870bc64ff5b3 to your computer and use it in GitHub Desktop.
Save SabrinaMarkon/0da6cf5304acb90f304d870bc64ff5b3 to your computer and use it in GitHub Desktop.
php-force-refresh-of-image-to-ensure-user-sees-most-recent-version.txt
If an application allows users to edit images, for example, once the image file is saved and the user returned to the page
showing their images (such as in the banner maker app), the behavior by default is that a cached version of an image is shown until
the user manually refreshes the page. This results in confusion because the person just edited the image and therefore expects those
changes to show right away.
To solve this problem it is a simple matter of creating a random string, such as the date/time:
$today = date("YmdHis");
Then in the HTML img tags, add ?$today to the end of the image src attribute.
For instance
<img src="user_image.png?$today> instead of just <img src="user_image.png">
This forces a refresh of the image so the user sees the most recent copy.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment