Roumazeilles.net

Script for 404 random page found

Some people have been wondering how I did the random selection of 404 page not found messages that are used on this web site.

So, here is the nice little PHP script that I use here to redirect randomly to one of the right pages:

  1.  
  2. <?PHP
  3.         $address_list = array(
  4.                 "/404/404-01.php",
  5.                 "/404/404-02.php",
  6.                 "/404/404-03.php",
  7.                 "/404/404-04.php",
  8.                 "/404/404-05.php",
  9.                 "/404/404-06.php",
  10. /*              "/404/404-07.php",      */
  11.                 "/404/404-08.php",
  12.                 "/404/404-09.php",
  13.                 "/404/404-10.php",
  14.                 "/404/404-11.php",
  15.                 "/404/404-12.php",
  16.                 "/404/404-13.php",
  17.                 "/404/404-14.php",
  18.                 "/404/404-15.php",
  19.                 "/404/404-16.php",
  20.                 "/404/404-17.php",
  21.                 "/404/404-18.php",
  22.                 "/404/404-19.php",
  23.                 "/404/404-20.php",
  24.                 "/404/404-21.php",
  25.                 "/404/404-22.php",
  26.                 "/404/404-23.php",
  27.                 "/404/404-00.php"
  28.         );
  29.         srand((double)microtime()*1000000);
  30.         $randomtopic = rand(0,count($address_list));
  31.         header(‘Location: ‘.$address_list[$randomtopic]);
  32.         exit;
  33. ?>

You can re-use it on your own web site if you want to. It’s free.

Other similar articles:



  • feed of the comments.

2 Responses to “Script for 404 random page found”

  1. Xtian Says:

    A couple of remarks:

    When you redirect an error page, don’t forget to set the error status in your page, otherwise the error is lost. You can add:
    header(”Status: 404 Not Found”);

    Why don’t you look for all files in the 404 directory and choose one of them?
    $dir = “somewhere/404/”;
    if (is_dir($dir)) {
    if ($dth = opendir($dir)) {
    while (($file = readdir($dth)) !== false) {
    if (preg_match(’/^404-.*\.php$/’, $file) == 1) {
    $address_list[] = $dir . $file;
    }
    }
    }
    }

  2. Yves Roumazeilles Says:

    That’s right, but did you notice that some (one) of the pages are not displayed? Actually, there are authorizations and copyright issues that limited the number of pages I accepted to go to the user. And this was changing pretty often (there is still one page not displayed because of this).

    Actually, your solution is nicer for sure.

Leave a Reply Comments


http://www.roumazeilles.net/

Copyright (c) 1999-2008 - Yves Roumazeilles (all rights reserved)

Latest update: 23-dec-08

Search provided by Google.com
Roumazeilles.net
Roumazeilles.net