Chrome caching 302 redirects

Posted by Thermionix on Server Fault See other posts from Server Fault or by Thermionix
Published on 2012-02-24T03:41:20Z Indexed on 2012/11/19 5:05 UTC
Read the original article Hit count: 366

Filed under:
|
|
|
|

I have a php script with is used to rotate banner images on a site.

Under Firefox/IE page refreshes will make another request and a different image will be returned.

Under Chrome, the request seems to be cached and only opening the page in a new tab will cause it to actually query the script.

I believe this used to work in older versions of chrome, I've tried a few different types of redirect codes all with the same result.

Any tips?

<img class="banner" src="/inc/banner.php" alt="">

~$ cat /var/www/inc/banner.php 
<?php

header("HTTP/1.1 302 Redirect");
header("Cache-Control: max-age=0, no-cache, no-store, must-revalidate");

//header('HTTP/1.1 307 Temporary Redirect');
//header("expires: none");
//header("expires: max");
//header("Cache-Control: public");

$folder = '../img/banner/';

$exts = 'jpg jpeg png gif';

$files = array(); $i = -1;
if ('' == $folder) $folder = './';

$handle = opendir($folder);
$exts = explode(' ', $exts);
while (false !== ($file = readdir($handle))) {
foreach($exts as $ext) { // for each extension check the extension
if (preg_match('/\.'.$ext.'$/i', $file, $test)) { // faster than ereg, case insensitive
$files[] = $file; // it's good
++$i;
}
}
}
closedir($handle); // We're not using it anymore
mt_srand((double)microtime()*1000000); // seed for PHP < 4.2
$rand = mt_rand(0, $i); // $i was incremented as we went along

header('Location: '.$folder.$files[$rand]); 
flush();
?>

curl output;

~$ curl -I -k https://example.net/inc/banner.php
HTTP/1.1 302 Redirect
Server: nginx/1.1.14
Date: Fri, 24 Feb 2012 03:23:46 GMT
Content-Type: text/html
Connection: keep-alive
X-Powered-By: PHP/5.3.10-1ubuntu1
Cache-Control: max-age=0, no-cache, no-store, must-revalidate
Location: ../img/banner/2.jpg

© Server Fault or respective owner

Related posts about php

Related posts about redirect