FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


IE8 not allowing new random image on each page (PHP and CSS)





eday2010
I am currently finishing up the redesign of my site. On the left side of the text is a div that does nothing but display a background image using CSS and PHP. I want it so that everytime someone goes to a new pages, this image changes. It works most of the time in Firefox, but it doesn't work in IE 8. I am assuming that the browser is just keeping the same style sheet loaded instead of refreshing it. So I want to know if there is a way for the site to force the refreshing with every page change.

Every page has

Code:
<div class="leftCol" id="random">


which is where the background image is displayed. id="random" refers to

Code:
#random {
   background-image: url(../images/random/rotate.php);
   background-repeat: no-repeat;
}


which is what places the image. And rotate.php is the following:

Code:
<?php
       $folder = '.';

    $extList = array();
   $extList['gif'] = 'image/gif';
   $extList['jpg'] = 'image/jpeg';
   $extList['jpeg'] = 'image/jpeg';
   $extList['png'] = 'image/png';
   
$img = null;

if (substr($folder,-1) != '/') {
   $folder = $folder.'/';
}

if (isset($_GET['img'])) {
   $imageInfo = pathinfo($_GET['img']);
   if (
       isset( $extList[ strtolower( $imageInfo['extension'] ) ] ) &&
        file_exists( $folder.$imageInfo['basename'] )
    ) {
      $img = $folder.$imageInfo['basename'];
   }
} else {
   $fileList = array();
   $handle = opendir($folder);
   while ( false !== ( $file = readdir($handle) ) ) {
      $file_info = pathinfo($file);
      if (
          isset( $extList[ strtolower( $file_info['extension'] ) ] )
      ) {
         $fileList[] = $file;
      }
   }
   closedir($handle);

   if (count($fileList) > 0) {
      $imageNumber = time() % count($fileList);
      $img = $folder.$fileList[$imageNumber];
   }
}

if ($img!=null) {
   $imageInfo = pathinfo($img);
   $contentType = 'Content-type: '.$extList[ $imageInfo['extension'] ];
   header ($contentType);
   readfile($img);
} else {
   if ( function_exists('imagecreate') ) {
      header ("Content-type: image/png");
      $im = @imagecreate (100, 100)
          or die ("Cannot initialize new GD image stream");
      $background_color = imagecolorallocate ($im, 255, 255, 255);
      $text_color = imagecolorallocate ($im, 0,0,0);
      imagestring ($im, 2, 5, 5,  "IMAGE ERROR", $text_color);
      imagepng ($im);
      imagedestroy($im);
   }
}

?>


I am not sure if it's a browser thing, or if it's the CSS, or the PHP. But I would like it to be a new random image with each page. Is there a way to ensure this (without JavaScript), and if so, how do I go about doing it?
jmraker
If the css part is written by the php program you can make the url unique like

Code:
echo '
#random {
   background-image: url(../images/random/rotate.php?r=' . time() . ');
   background-repeat: no-repeat;
}';

so on every page load it has a different url

or you can try to turn off browser caching in rotate.php
eday2010
That didn't workI put the code near the end of the PHP code, and it did the same thing as before: the image would not change. I put the code higher up before all the if statements, but then no image loaded.

How would I go about turning off browser caching in the rotate.php file? I don't actually know how to code PHP (this code was written by someone else).
jmraker
There's a few examples of telling the browser to disable the cache in the comments on http://us3.php.net/manual/en/function.header.php

the header(....) statements need to be before any output is sent.
eday2010
None of the ones I tried worked. Perhaps I was not putting them in the right spot (at the very top of the page), or perhaps I didn't modify something that needed to be changed. I don't know how to code PHP, so if anyone can help with getting IE to not cache anything, that would be great.

A couple people on DynamicDrive said that the site was working fine in IE8 with changing the picture, but it is not working for me. If anyone wants to try it themselves, the address is www.wnstudios.ca/contact (every page but the home page is up).
sonam
Without javascript, ajax or meta tag refresh you cannot rotate images. PHP is server side scripting and need some browser input for rotating images. You can look on my site for "Hercules banner/image rotator" what is working with meta tag refresh. It is little bit iritable in IE because create one noise. I will do it next generation with ajax but I don't know when because I heve lot of learning for my new job.

Sonam
eday2010
Solved.

Putting

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


at the top of my rotate.php file prevents the file from being cached, and thus allows my site to function properly in IE.

Hooray!
Related topics
PHP Tutorials
PHP image
Random Image
comment integrer une page html dans pcnuke
PHP Image Questions
Help me put adsense on Forum 100 Frih$!!!!
Display random image on foreign site
comment integrer un script dans une page php
PHP script problem
How change the background-image of a page using CSS?
random image code?
I want to output an img url from a random image script
Having browser not pull files from the cache.
Common IDE Platform for PHP, CSS, HTML and Specially JQuery
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.