Making sure an image is refreshed/reloaded every time a page is viewed

If you have an image on your website that is reloaded from some source (like a web cam) every so often, your returning visitors will not see that latest copy of the image when they return to the page.

This problem is caused by the browser's cache. Your visitors will see the image that was saved in their browser's cache the first time they visited the web page.

Do you want to make sure your visitors always see the latest version of the image (essentially disabling the browser's cache for the image)?

This is easily done with PHP.

All you need to do is append a unique query string to the image URL. Having a unique query string each time the page is viewed will force the browser to reload the image every time.

This query string needs to contain a value that is unique on each visit.

In my example, I use a timestamp as the query string argument. This ensures the query string will be unique every second... forever.


Very simple.

Very cool.

Very PHP.


Following is the code.

Here is the original static link to the image:

PHP Code
<img src="http://russtanner.com/Cams/OfficeCam_Russ.jpg"> 


Now, here is the link after it's been updated to automatically refresh / reload each time the page is viewed:

PHP Code
<img src="http://russtanner.com/Cams/OfficeCam_Russ.jpg?<?php print date('YmdHis'); ?>"> 


Remember:

1. Don't forget the question mark immediately after ".jpg"!

2. Make sure this web page file name ends with ".php" (a common mistake).



Example

Here is a page that employs this method on a webcam:

http://russtanner.com/Webcam/

Last edited by Russ; 03/29/13 01:46 AM.

The Captian
Today they call you "crazy". Tomorrow they call you "ahead of your time."
Global Skywatch Learn about Chemtrails - You're breathing them now!
OnlyTheBestHerbs.com World-class supplements
Mercury Talk Why you are sick.
OneUp Domains Domains, Hosting, Email
1-800-358-4278 (U.S. & Canada)