5

My site is designed to be a funny picture site, when the user hits the random button a PHP code on the same page generates a new random picture, this is how it is supposed to work. I however have to hit the F5 button to get a new image.

I was reading on another question that people use a get date and get time query string generated at the end of the link to avoid browser caching, I however can not figure it out for the life of me.

I am not very good with php so please speak as if I only know the basic webpage structure. Thank you!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Clayton Moss
  • 51
  • 1
  • 3

3 Answers3

6

What you are describing is called a cache breaker and is usually a random string or a timestamp appended to the url. When you are referencing your image, prepend it like this:

echo get_random_image_url() . '?' . time();

This will result in an url looking like this:

http://your.server.com/random.jpg?1355862360

Note: get_random_image_url is just an example, but i'm sure you get the idea.

This thread may be of interest: How to force a web browser NOT to cache images.

Community
  • 1
  • 1
alexn
  • 57,867
  • 14
  • 111
  • 145
  • its actually referencing a php file that pulls a random funny image from my hosting. I am sorry, i am pretty good with CSS and html but i just started learning a few months ago. Any more help would be greatly appreciated. – Clayton Moss Dec 19 '12 at 07:38
1

i think using headers is better than the url trick

<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
?>

http://php.net/manual/en/function.header.php

0

It is very easy to be solved: for example, Check the following two links:

http://cdn.arstechnica.net/wp-content/uploads/2012/10/06_Place_20773_1_Mis.jpg

http://cdn.arstechnica.net/wp-content/uploads/2012/10/06_Place_20773_1_Mis.jpg?randomValue

Both of the two links will be open the same image.

This is your solution! You have to add at the end of your image file name a random value:

image.png?<?php echo someRandom();?>

This community or Google for a way to write a function that gemnerates random values.

Also there is solution using , suupose the following

<img id="funny" src="scripts/php_rand_image.php" />
<a href="javascript:changeImage('funny')">Get another image</a>
<script>
function changeImage(ob){
image = document.getElementById(ob)
d = new Date();
image.src = image.src+'?'+d.getTime();
}
</script>
SaidbakR
  • 13,303
  • 20
  • 101
  • 195
  • i attempted your response because it seemed to be the easiest, thank you, but when i put that at the end of the URL, ex:"" in the index.php file the page no longer displays. Any feedback? – Clayton Moss Dec 19 '12 at 07:31
  • You have to notice that: someRandom() is a custom function that you have to write. The someRandom() should be suffixed to the image url. In the other hand, I recommend to use the Javascript dependent solution. – SaidbakR Dec 19 '12 at 11:37