« Web, HTML, Tech Forum

Randomize Backgrounds TUTORIAL [Requires PHP Capable web server]

Here's how to set up a web server to pick a random background and how to integrate it into spacehey.


For an example of how it works check out my profile.

So to start off you will need a PHP capable web server such as PHP on Apache, NGINX or windows server or a hosting service.

Now if you got this far I'm assuming you know how to put things in folders on a server so in your web root place a folder called BG and place you backgrounds in here.

The fun part now the code. This basically spits out some CSS that you can import.

So here's the PHP that needs to be on your server as background.php obviously replace the links in the $photoAreas array you can add as many as you want.

    <?php
    $photoAreas = array("ADD LINKS TO YOUR IMAGE HERE", "ADD LINKS TO YOUR IMAGE HERE", "ADD LINKS TO YOUR IMAGE HERE");
    $randomNumber = rand(0, (count($photoAreas) - 1));
    header('Content-Type: text/css');
    echo '
    body{
    background-image: url("'.$photoAreas[$randomNumber].'") !important;
    }
    ';

After adding all that and verifying the page loads without errors then just import it in CSS 

    @import url('PATH.TO.BACKGROUND.PHP');


Report Topic

1 Reply

Reply by Elkins6

posted

Virgin Media's Refer a Friend program is a fantastic way to spread the joy of high-quality entertainment and connectivity. I say you can get virgin media contact number and learn more information about the media. By referring a friend, you can both enjoy exclusive benefits, from discounts on broadband and TV services to exciting rewards. It's a win-win for both existing and new customers, making it easier than ever to share the Virgin Media experience with your loved ones.


Report Reply