Saturday 24 September 2011

Can I use PHP to display a random background when my website loads?

I currently have 7 background images as jpeg files. I would like a random picture to be displayed when the browser loads the page. Is this possible using PHP? Can someone provide the code? Also the page is named as index.html would I then need to change this to index.php?Can I use PHP to display a random background when my website loads?Keep it simple and fast!



Name your images image bg1, bg2, bg3 etc (for my example they are stored in /images/ directory).



Add styles to your css sheet:



.bg1 {background: white url(/images/bg1.jpg) no-repeat center;}

.bg2 {background: white url(/images/bg2.jpg) no-repeat center;}



... etc (you need 7 of them in this case). Or (less useful) put the styles in the %26lt;head%26gt; of every page which uses the random background image ( inside %26lt;style type=%26quot;text/css%26quot;%26gt; %26lt;/style%26gt; tags).



Add to the %26lt;head%26gt;:



%26lt;?php

$randombg = rand (1, 7);

?%26gt;



In the %26lt;body%26gt; apply the random background image to any element you choose:



%26lt;p class=%26quot;bg%26lt;?php echo%26quot;$randombg%26quot;;?%26gt;%26quot;%26gt;



This produces a very streamlined output eg: %26lt;p class=%26quot;bg3%26quot;%26gt;



This method takes virtually no time to process - and your source code will reveal nothing at all - just nice clean html , which means that you have a higher ratio of content to code, to keep the search engines happy!



CHANGING THE PAGE NAME?



KEEP your index.html file and ADD a new index.php file and use php or htaccess to set a permanent redirect to your new php page. Don't get rid of your index.html file - you will mess up your search engine ranking. Also, anyone who has bookmarked your home page will get an error message instead of being redirected to your new page.



Alternatively, use htaccess to parse html files as php files.
Can I use PHP to display a random background when my website loads?
Yes but it's not recommanded since it'll increase your page load time ... Make sure your images are 200k or less. here:



http://www.famouswolf.com/weblog/view/ar鈥?/a>
Can I use PHP to display a random background when my website loads?
I have the same question ;-)

I looked around and tested several PHP codes but they didn't result. I'll keep you updated.



Hope someone else contributes here as I need that code too.



NOTE: the images should be in a specific folder, and the PHP code should randomly show all pics in that folder. This way, you are not limited to 7, and can add and delete pics whenever you want.



Yes, you will have to change to index.php



-----

Update: the message above looks great, but it doesn't work for me. Is it because the directories are protected??
It is 'of course' possible, I have the code for it at home (I once used it for a website I designed) and I'll send it to you later today if I don't forget it. :-) Yes, you'll have to rename the .html file to .php.
Hey I just picked up this random snippet



%26lt;?php



/*

* Name your images 1.jpg, 2.jpg etc.

*

*/



// Change this to the total number of images in the folder

$total = %26quot;11%26quot;;



// Change to the type of files to use eg. .jpg or .gif

$file_type = %26quot;.jpg%26quot;;



// Change to the location of the folder containing the images

$image_folder = %26quot;images/random%26quot;;



// You do not need to edit below this line



$start = %26quot;1%26quot;;



$random = mt_rand($start, $total);



$image_name = $random . $file_type;



echo %26quot;%26lt;img src=\%26quot;$image_folder/$image_name\%26quot; alt=\%26quot;$image_name\%26quot; /%26gt;%26quot;;



?%26gt;



This code will probably be best in an header file or near the start of your php page. The end code would need to be changed a little bit but you should be able to figure this out.



Good Luck

Paul
1. Have your images called, say: bkg1.jpg, bkg2.jpg etc... and located in the same directory as the calling page.

2. In the calling page:

%26lt;?php

$maxbkg = 7; // this is the max number of pictures

$i = rand(1,$maxbkg); // gets a random number from 1 to 7

$bkgpic = %26quot;bkg%26quot; . $i . %26quot;.jpg%26quot;; // build the bkg name (ie bkg3.jpg)

echo (%26quot;%26lt;BODY ... BACKGROUND='%26quot; . $bkgpic . %26quot;' %26gt;\r\n%26quot;);

?%26gt;

(after the = it is ' followed by %26quot;, and before the end, it is %26quot; followed by ' . No space in between. The \r\n is not necessary, but makes the clode cleaner when you look at it in HTML)

3. The calling page must end with %26quot;.php%26quot;, not %26quot;html%26quot;...

That's it!