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
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. 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.