This function returns the path to a random image. The cookie will exist until
the browser window is closed. ( view demo )
=================================== PHP ===================================
<?php
/*
Set a random background image per "visit"
Bruno Correia, June 2009 http://bcdc.us
==========================================
*/
ob_start();
function cookie_random_img($min, $max) {
$path = 'images';
$file_prefix = 'background_';
$extension = '.jpg';
$n = mt_rand($min, $max);
if (isset($_COOKIE['rand'])):
$output = base64_decode($_COOKIE['rand']);
else:
$output = base64_encode($path . '/' . $file_prefix . $n . $extension);
setcookie("rand", $output, 0, "/");
$output = base64_decode($output);
endif;
return $output;
ob_end_flush();
}
?>
=================================== HTML ===================================
<html>
<head>
<style type="text/css" media="screen">
body { background: url('<?php echo cookie_random_img(1, 5); ?>'); }
</style>
</head>
<body>
<!-- background image -->
</body>
</html>