Categories: Development

Have a random or new background image each page load

If you wondered how to do a new or random background image each time a web page is loaded here is how. Put the PHP script at the top of your web page and then the next segment of code inside your

section. You can have as many background images as you want, just state them in the array (note I was storing the images in the folder bg_images).

The PHP script:

$bg = array('bg1.jpg', 'bg2.jpg', 'b3.jpg', 'etcetc.jpg'); // array of background image filenames
$i = rand(0, count($bg)-1);
$selectedBg = "$bg[$i]";

The PHP inside HTML:

 

Do this allows you to easily have a random or new background image each time your page is loaded.

Share

Recent Posts

Kennington reservoir drained drone images

A drained and empty Kennington reservoir images from a drone in early July 2024. The…

2 years ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

2 years ago

Early September spring shots

Some early September spring shots from a bushwalk, It is still too early for wildflowers.

3 years ago

Cloning an NVMe to SK Hynix Platinum P41 NVMe

I recently needed to clone over a smaller NVMe to a 2TB SK Hynix Platinum…

3 years ago

Using Laravel’s deleteDirectory to delete folders and their contents

If you ever needed a simple solution in Laravel to delete an entire directory and…

3 years ago

How to allow only 1 user to register in a Laravel application

How to enforce only one user to register or be registered in a Laravel web…

3 years ago