1

I have set up a background changing script, which works just fine on one of my other web sites and when I have tried to do it on this new one (http://bit.ly/1Hwbn94), it just shows the first background image and does not randomly change it every 20 seconds (after clicking on another page).

Did I mess something up with the PHP code? I cannot find the error up to now. Maybe I have to add something to the header.php?

PHP code in random_img.php

<?php
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header('Content-Type: image/jpeg');
$bilder = glob( "../showcase/images/*.jpg" );
$seed = floor(time()/20); 
srand($seed); 
$random_image = $bilder[rand(0, count($result)-1)];
header('Location:'.$random_image.'');
?>

CSS code

body { 
width: 100%;
height: 100%;
margin: 0;
padding: 0;
background: url("../random_img.php") no-repeat top left fixed; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover; background-attachment: fixed;
}
2
  • the path is right ? Commented Apr 24, 2015 at 13:22
  • Well, I guess so. If the path would not be right, there would be no background picture at all, but it is loading the first one. Commented Apr 24, 2015 at 13:26

3 Answers 3

1

At this line $result is undefined

$random_image = $bilder[rand(0, count($result)-1)];

Correct

$random_image = $bilder[rand(0, count($bilder)-1)];
Sign up to request clarification or add additional context in comments.

1 Comment

Haha, yeah, it is a pest. Especially, when reading stuff allover thousands of times. :D
0

The problem maybe is the cache of CSS.

Try in your CSS

 background: url("../random_img.php?nocache=1") no-repeat top left fixed;

Regards...

Comments

0

Try doing this, there may be a php error happening in the background that you wont be able to see if you have your headers set as image/jpeg. Remove all the custom headers and try the following code in a php file in the same directory and see if you get any errors.

$bilder = glob( "../showcase/images/*.jpg" );
$seed = floor(time()/20); 
srand($seed); 
$random_image = $bilder[rand(0, count($bilder)-1)];
echo $random_image;

If all is setup correctly, your images are in the right directories you should see a string of the image chosen.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.