No announcement yet.

Random php background image (header)

  • Filter
  • Time
  • Show
Clear All
new posts

  • Random php background image (header)

    There are dozens of post detailing php_include template to use a php file in your forum. But, none of them worked for me to get a random background or image to work..

    To create a random background image in the header:
    1. Create a folder in your forum /images directory, I called mine header (/forum/images/header)

    2. In that folder I put a php file with the following code (found in this forum) and called it header.php
            By Matt Mullenweg >
            Inspired by Dan Benjamin >
            Latest version always at:
             [i]// Make this the relative path to the images, like "../img"          or "random/images/".
            // If the images are in the same directory, leave it blank.
            $folder = '';[/i]
             [i]// Space seperated list of extensions, you probably won't have to          change this.
            $exts = 'jpg jpeg png gif';[/i]
             [i]$files = array(); $i = -1; // Initialize some variables
            if ('' == $folder) $folder = './';
            $handle = opendir($folder);
            $exts = explode(' ', $exts);
            while (false !== ($file = readdir($handle))) {
            foreach($exts as $ext) { // for each extension check the extension
            if (preg_match('/\.'.$ext.'$/i', $file, $test)) { // faster than ereg,          case insensitive
            $files[] = $file; // it's good
            closedir($handle); // We're not using it anymore
            mt_srand((double)microtime()*1000000); // seed for PHP < 4.2
            $rand = mt_rand(0, $i); // $i was incremented as we went along[/i]
             [i]header('Location: '.$folder.$files[$rand]); // Voila!
    3. Put some images in the folder

    4. In your forums header template, I added a background in the table with the header.php file, like:

    <!-- logo -->
    <a name="top"></a>
    <table border="0" width="$stylevar[outertablewidth]" height="100" cellpadding="0" cellspacing="0" align="center" [b]background="images/header/header.php"[/b]>
    <td align="$stylevar[left]" width="500" valign="middle"><a href="" title="My Forum"><img src="$stylevar[titleimage]" border="0" alt="$vboptions[bbtitle]: Title of My Forum" /></a></td>
    	<td> </td>
    <!-- /logo -->
    <!-- content table -->
    Now, every reload shows a different background image and I didn't need the php_include template. You can also use this to have a random image vs. a background.

    I'm sure you could find this in the forum, but all I found was Steve referring users to read the manual and look at the php_include tutorial on

    - BT

  • #2
    Thanks for posting this, but it should be posted on since it involves a custom PHP script.


    widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.