Jump to content

Reloading images on one page


scuff

Recommended Posts

I have a php generated image:

 

(number.txt contains a number 1 or 2)

 

pic.php

<?php
// Set the content-type
header('Content-type: image/bmp');
// Create the image
$im = imagecreatetruecolor(80, 80);

$count_my_page = ("number.txt");
$hits = file($count_my_page);


if ($hits[0] == 1) {
$im = imagecreatefrompng("image1.png");
$hits[0] ++;
$fp = fopen($count_my_page , "w");
fputs($fp , "$hits[0]");
fclose($fp);
}else{
$im = imagecreatefrompng("image2.png");
$hits[0] --;
$fp = fopen($count_my_page , "w");
fputs($fp , "$hits[0]");
fclose($fp);
}

// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 1, 1, 1);

// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>

 

And I have another page

 

index.html

<img src="pic.php">
<img src="pic.php">

 

Basically, I need one of the images on the index page to be image1 and then the second image to be image2, but it must be contained within the php file because I am unable to edit index.html. Is there anyway to do this? The method I have now where it reads a file to see if it's value is 1 or 2 doesn't seem to compute each time the image is generated. I have tried

header("Cache-Control: no-cache");

header("Pragma: no-cache");

in the image code to force it not to cache but to no avail. Can anyone solve this?

 

Link to comment
https://forums.phpfreaks.com/topic/236499-reloading-images-on-one-page/
Share on other sites

I think your problem is probably the browser caching the image.  Only way to really keep it from doing that is attaching a random number to the request, like

 

<img src="pic.php?n=123">

 

usually people just do something like this:

 

<img src="pic.php?n=<?php echo time(); ?>">

 

which just uses the current unix timestamp as the number.

 

...but you said you can't change index.html.  Well..I'm afraid you're out of luck then.  Image caching happens in the browser, so you're going to have to change index.html like above or have a meta tag or something specifically telling the browser not to use cached images. 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.