Surprise Me!

Well not really, but a DP’er wanted to know how to pull a random link from a directory and direct her visitor there. Not my style of surfing but hey…

You can see Surprise Me working over at Top 100 vBulletin, just look down the menu on the left hand side.

Here’s the code [php]< ?php // PHP Surprise Link Script /* -------------------------- May 2006 or visit for updates, visit: Instructions Edit surprise.php to connect to the database Set the three variables to the name of your links table and the correct column names Upload surprise.php to your server Add a link to your page such as Surprise Me

Once it’s working add some formatting to the intermediate page.

You can see it working at

This code is released unto the public domain

include ‘dbsetupscript.php’;

$surprise_table = $linkstable;
$surprise_url = ‘url’;
$surprise_name = ‘title’;

//initialise the connection to the database if it’s not already open
$sql = “select `{$surprise_url}`, `{$surprise_name}`
from `{$surprise_table}`
order by rand()
limit 1 “;
$result = mysql_query($sql) or die( mysql_error(). ‘
‘ . $sql);
$row = mysql_fetch_array($result);
echo “

We hope you enjoy your surprise visit to {$row[$surprise_name]}



Recent Comments



  1. April 16, 2007

    Just one question..
    why not use DEFINE(“SURPRISE_URL”,”url”); instead of variables?
    Maybe defines are better/faster.

  2. April 16, 2007

    Defines are for variables which might be used anywhere by any script.

    If you put this code into a function the variables will cease to exist when you go back to the parent function – and that’s appropriate. But if they were defined they would be available over and over…

  3. April 16, 2007

    Right. This is not good.
    I use defines for table names because it must be available to all the functions and classes usually.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.