Surprise Me!

May 30th, 2006 by Sarah King

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:
  1. <?php
  2. // PHP Surprise Link Script
  3. /* -------------------------- May 2006
  4. or visit www.itamer.com
  5. for updates, visit:
  6. http://www.itamer.com/surprise-me/282/
  7. Instructions
  8.   Edit surprise.php to connect to the database
  9.   Set the three variables to the name of your links table and the correct column names
  10.   Upload surprise.php to your server
  11.   Add a link to your page such as <a href='/surprise.php' target='_blank'>Surprise Me
  12.   Once it's working add some formatting to the intermediate page.
  13.   You can see it working at http://www.top100vbulletin.com
  14.   This code is released unto the public domain
  15. */
  16.  
  17. include 'dbsetupscript.php';
  18.  
  19. $surprise_table = $linkstable;
  20. $surprise_url = 'url';
  21. $surprise_name = 'title';
  22.  
  23. //initialise the connection to the database if it's not already open
  24. $sql = "select `{$surprise_url}`, `{$surprise_name}`
  25.     from `{$surprise_table}`
  26.     order by rand()
  27.     limit 1 ";
  28. $result = mysql_query($sql) or die( mysql_error(). '<br />' . $sql);
  29. $row = mysql_fetch_array($result);
  30. echo "<html>
  31. <head>
  32. <meta http-equiv='refresh' content='0;url={$row[$surprise_url]}'>
  33. </meta></head>
  34. <body>
  35. <p>We hope you enjoy your surprise visit to <a href='{$row[$surprise_url]}'>{$row[$surprise_name]}</a></p>
  36. </body>
  37. </html>";
  38. ?>

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Twitter
  • StumbleUpon
  • Mixx
  • Google Bookmarks
  • Blogplay
  • Add to favorites
  • Sphinn
  • NewsVine
  • Propeller
  • Yahoo! Bookmarks
  • Yahoo! Buzz

3 Responses to “Surprise Me!”

  1. maurizio says:

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

  2. Sarah King says:

    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. maurizio says:

    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

You must be logged in to post a comment.