Jim,
I want to design a page where the background is different every time the page is visited.
I know the starting code would be
<body background="image.gif" bgcolor="#??????"> and I would store a bunch of images I want as backgrounds with my site at my webhost, but how would I get the code to use a different image every time some one calls for this page? I’m pretty sure I’m going to have to use that PHP no caching code you showed me. <meta name="Expires" content="<? echo gmdate (‘D, d M Y H:i:s’, time()+00*08*00)?>">
I went to http://www.htmlcodetutorial.com/quicklist.html but couldn’t find anything.
Roger

Hi Roger,
Here is what you’ll do, and yes, you need php.
Use this:
in place of the background statement you are use to.
Now, create a file and call it background.php and place this code into it:
Wherever you place the the randombackground.php file, create a subdirectory called /rimages
Now, place your images in that directory and rename them starting from 1, so if you have blueberry.jpg, apple.jpg and garlic.jpg, then rename them to 1.jpg, 2.jpg and 3.jpg. If you are not using jpg files, no problem, just change $file_type = ".jpg"; to $file_type = ".your extension";
Also, change the $Total variable above to the total number of images you have.
Here is an example of the Background Randomizer for you:
The code I show above is without a style. In my example page I created, I use a style. The image is a background image and centered and does not repeat. The only way to do that is by using a simple style statement. Here is the code that makes the page:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Background Randomizer for Roger</title> <style type="text/css"> html { height: 100%; } body { background-image: url(<?php include 'randombackground.php'; ?>); background-repeat: no-repeat; background-position: center; } </style> </head> <p><?php include 'randombackground.php'; ?></p> </body> </html>Does that answer your question?
Jim
<body background="<?php include "randombackground.php"; ?>"></body>
Using both NVU and Ultradedit I don’t get that PHP symbol, I get: ">
when viewing the page. The tidy function says randombackground doesn’t have a value and a couple of compounded errors.
I pasted the background.php code into a .txt file than changed the name from .txt to .php.
Roger
A few things to try:
Try executing / running just the randombackground.php file and see if you get any errors.
Windows versions of PHP prior to PHP 4.3.0 do not support accessing remote files via this function, even if allow_url_fopen is enabled – not sure what you are running.
If you are still having problems, then you can always add this to the top of your page (before all other code):
Then, change the style statement to:
<body background="<?php echo "$image_folder/$image_name"; ?>">
and don’t worry about creating the randombackground.php file or calling it.
Let me know if that fixes the problem.
Regards,
Jim.
Also – if you have a page you are testing, let me know the name of that page and I’ll take a look at the code.
Jim,
I’m still getting that error "<body> escaping malformed URI." so I can’t be sure the code is working.
This is waht I have
<?php
$total = "4"; // Number of images you’ll be using
$file_type = ".jpg"; // use .gif, .png, etc if not using .jpg and NAME them 1.jpg, 2.jpg, 3.jpg, etc.
$image_folder = "rimages"; //folder where images are located, use a unique folder for this
$start = "1";
$random = mt_rand($start, $total);
$image_name = $random . $file_type;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN">
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
<title>newpage</title>
</head>
<body background="<?php echo ‘$image_folder/$image_name’; ?>">
</body>
</html>
Sorry about this Roger, but change:
to:
and give that a try.
Regards,
Jim.
Jim,
This is what I have:
<?php
$total = "4"; // Number of images you’ll be using
$file_type = ".jpg"; // use .gif, .png, etc if not using .jpg and NAME them 1.jpg, 2.jpg, 3.jpg, etc.
$image_folder = "rimages"; //folder where images are located, use a unique folder for this
$start = "1";
$random = mt_rand($start, $total);
$image_name = $random . $file_type;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN">
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
<title>newpage</title>
</head>
<body background="<?php echo "$image_folder/$image_name"; ?>">
</body>
</html>
And it produces: ">
in Ultra Edit, NVU, Firefox, and IE. There’s some glitch in there somewhere.
Thanks,
Roger
websecurity.mobi/testing/random-background.php is a page with the exact code you have post just above.
It checks out on a validator, so I’m not sure why the problem.
What is your url (of the page that is producing this error?)
If you post this phpinfo in the body of the document, what happens? What version of php is reported?
Regards,
Jim.
Jim,
/gpage2.html.
I just noticed that when I put this code online
<?php
$total = "4"; // Number of images you’ll be using
$file_type = ".jpg"; // use .gif, .png, etc if not using .jpg and NAME them 1.jpg, 2.jpg, 3.jpg, etc.
$image_folder = "rimages"; //folder where images are located, use a unique folder for this
$start = "1";
$random = mt_rand($start, $total);
$image_name = $random . $file_type;
?>
<!DOCTYPE html PUB ……… etc., etc…..
There’s no "> online but when I want to view the compiled code loacally it’s there: ">.
Online there’s no Tidy/Firefox errors but offline with Tidy/Firefox and Ultraedit I get
"HTML Tidy Parsing …
line 1 column 1 – Warning: specified input encoding (utf-8) does not match actual input encoding (utf-16)
line 15 column 1 – Warning: <body> attribute "$image_folder/$image_name";" lacks value
line 15 column 1 – Warning: <body> attribute "?" lacks value
line 15 column 1 – Warning: <body> escaping malformed URI reference
Info: Document content looks like HTML 3.2
3 warnings, 0 errors were found! "
I don’t see a function on UltraEdit for compiling PHP and HTML together. Maybe that’s the problem. But those errors on the two webeditors prducing ">
When I first Open UltraEdit is asks me all the types of files I might work with. I checked them all but didn’t see .PHP. , although this is the first time I’ve had trouble.
Thanks Jim,
Roger
Jim,
When I run the code in Notepad using "Get PHP help", This page comes up
ht tp: //us3.php.net/manual-lookup.php?pattern=%2520random&lang=en
Roger
Just checking in to see if you received my email and added the content to the page – let me know.
Also – the products came in today, so tomorrow, I’ll start taking them – we’ll see if we can’t get that garbage pick up service kicked into high gear and increase the HDL
Regards,
Jim.
Jim,
See /gpage.html (and my gmail).
Roger
I wasn’t following before; when you create a file locally and try to run it, such as creating a test.htm on your c: drive and then opening it with IE or Firefox, the you’ll get the ">
This happens because there is no php engine running in the background to interpret the php commands into html for the browsers. When you place php code inside a webpage, the web server executes the code behind the scenes and then creates the final html the browser sees.
In that example I gave you, you’ll see that when you place the php code on a page handles by your server, the source code (when you view it from your browser, not the server) will have only html in it.
When you create a page locally and try to run it with code inside, you’ll get all kinds of strange output; this is because the browser doesn’t know what to do with the code.
So, test on your server when working with php code and you’ll not get those errors.
Regards,
Jim.
Jim,
Should I work with "> as if it weren’t there?
Roger
Yes, and if you are ever not sure, run it on a test page on your server rather than your local drive.
Regards,
Jim.
Jim,
That clears that up. Thanks!
Roger