Jump to content

Random background image...


Jonas

Recommended Posts

Only one thing I'm curious about, is it possible to use php in a way so that you still get structured html when you look at the output html code? I'm kind of a neat freak that way... :)

Works like this:<html><head><?php //php code here?></head><body></body
Link to comment
Share on other sites

Works like this:<html><head><?php  //php code here?></head><body></body

Hmm, and what about if I use the echo statement? Do I put the spaces in front of the echo statement?
Link to comment
Share on other sites

bah, that's what I forgot: Opera testing.I'll get to work on a PHP version. That is why I usually prefer server-side scripting, it'll always work for the browser :)

Link to comment
Share on other sites

bah, that's what I forgot: Opera testing.I'll get to work on a PHP version. That is why I usually prefer server-side scripting, it'll always work for the browser :)

But... I've already got a script that works now, the one choco provided. Thanks guys, but I don't need anything else I think. Only thing that would be cool is if there's a way to prevent caching so that it really is random bg for each page, not just once, but on the whole.
Link to comment
Share on other sites

You can do that with cookies. If you'd really like, I can make it so that when it's got a background, it sets a cookie with it's value to the background. The next time it opens the page, it checks if the bg image it's supposed to put is not in the cookie, meaning it's different from last time. Do you want that?And my god, my php worked. I can't believe it. This really is amazing to me :)

Link to comment
Share on other sites

To stop caching
<?php header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); header('Pragma: no-cache'); ?>

Does it matter where in the document I place that script?
Link to comment
Share on other sites

Yes, the header() function must be called before anything else is sent to the web browser. If your web page receives any HTML or even blank space the header() function will not work.

<html><?php/* This will give an error. Note the output * above, which is before the header() call */header('Location: http://www.example.com/');?>

Link to comment
Share on other sites

Jonas if you havent already found a resolution you can use my php code. I wont remove it no worries :) Just be sure that you have all of the random images in one folder with no other non-image filetypes in it.

<html><head><title></title><?php$dir = "/home/DOMAIN/public_html/RANDOMIMAGESFOLDER";$domain = "http://www.DOMAIN.com";$folder = "RANDOMIMAGESFOLDER";$imgs = array();// opens image directoryif (is_dir($dir)) {if ($dh = opendir($dir)) {while (($file = readdir($dh)) !== false) {if(!is_dir($file)) {$imgs[] = $file; }}closedir($dh);}}$rand = array_rand($imgs, 1);$rand_image = $imgs[$rand];echo "<style type=\"text/css\">     body {       background: url(\"".$domain."/".$folder."/".$rand_image."\");     }   </style>";?></head><body></body></html>

edit: heh... i guess i only read the first page, oh well.

Link to comment
Share on other sites

  • 4 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...