Reputation: 6474
I have the following code in a php file to create a random image using php:
header("Content-type: image/png");
$my_img = imagecreate(rand(200,400), rand(50,100));
$background = imagecolorallocate($my_img, 0, 0, rand(1, 255));
$text_colour = imagecolorallocate($my_img, rand(1, 255), rand(1, 255), 0);
$line_colour = imagecolorallocate($my_img, rand(1, 255), rand(1, 255), 0);
imagestring($my_img, 4, 30, 25, "Random Text Here", $text_colour );
imagesetthickness ( $my_img, 5 );
imagepng($my_img);
imagecolordeallocate($line_color);
imagecolordeallocate($text_color);
imagecolordeallocate($background);
imagedestroy($my_img);
I add the image using an html file like this:
<img src="myphpfile.php">
If I do that more than once:
<img src="myphpfile.php">
<img src="myphpfile.php">
<img src="myphpfile.php">
<img src="myphpfile.php">
<img src="myphpfile.php">
it generates the same image 5 times. How can I use the php file to display a different image?
Upvotes: 2
Views: 80
Reputation: 702
The image is being cached. Add no cache headers:
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); header("Cache-Control: post-check=0, pre-check=0", false); header("Pragma: no-cache");
Upvotes: 2
Reputation: 17171
Try adding a random query string to prevent the browser from thinking it's the same image and caching it.
<img src="myphpfile.php?1">
<img src="myphpfile.php?2">
<img src="myphpfile.php?3">
<img src="myphpfile.php?4">
<img src="myphpfile.php?5">
Upvotes: 2