Phil’s Hobo Humor and Insights

Hobo humor, funny stories, and maybe something useful too.



Free Perl Script - Random Quote Generator for Your Website

December 27th, 2006 by Phil Stewart

I recently added a random quote to the bottom of this site. Each time a page on this website loads, a different random quote is displayed. This was fairly easy to add, didn't take much time, and I hope it will be a nice little humorous touch.

There are many uses for a random quote generator, not simply to display a funny or goofy phrase. You may have a tagline, and want an easy way to change it from time to time. Or you may have a list of links, and want to show them randomly. The script running the quotes at the bottom of this site is custom, and very simple in nature. It will take regular text and/or HTML code from a regular text file. This makes it easy to maintain, and add or remove items from the list. Plus, this means you could even make a list of images, and use this script to randomly display them on your site. So it's not only a random quote generator, it can also be used as a random image generator.

Now to the good stuff. I am making the random quote generator available for free on this site. It is written in PERL and is what is known as a CGI script. All this means is that it was made to run on the web. As long as your webhost allows CGI scripts, there should be no problem running this little program on your website.

So, if you want a random quote (or image) generator on your website, the first thing you want to do is download the script.

Download Random Quote Generator

I have included the random quote script and sample quotes in a “zip” file. When the downloaded file is unzipped, a directory called “random-quote-generator” is created. Inside this directory are two files, the PERL script (“random-quote.cgi”) and the sample quotes file (“quotes.txt”). This is all that is needed to run the random quote generator.

To get started, open quotes.txt in a text editor and enter your quotes, taglines, or other HTML you would like displayed. An example for random quotes may look something like this:

The early bird may get the worm, but the second mouse gets the cheese.
One time I woke up taped to the soda machine.
Here is another quote with something apparently witty or funny.

Make sure to put each item on its own line.

Once you have entered your own random quotes (you can always add more later), it's time to upload the files to your website. It doesn't really matter where you upload the files to, but they must be in the same directory, the directory must be web-accessible (meaning you can view the files on the web), and you must be able to run CGI scripts from this directory.
When both files have been uploaded, the CGI script must be made executable (so it can run over the web). This can be done through most FTP programs, or by typing the following “chmod” (change mode) command at the command line prompt:

chmod 755 random-quote.cgi

At this point you should be able to view the script in a web browser and see one of your quotes randomly displayed. An example is at http://www.philstewart.org/random.cgi

If you run into problems, it is possible PERL is installed in a different location on your webserver, or CGI scripts cannot be run in the directory where the script is. Solutions to these problems are beyond the scope of this post, but if you run into issues, let me know and I'll see if we can work it out.

Now that the random quote generator is running, we need to include it in a webpage. There are several ways the random quotes can be included in your site, but it will mostly depend on how your site is setup, and the types of files you are using.
One method for displaying random quotes is by loading the CGI script in a frame, but that seems kind of cumbersome. So here are two other easy ways to add the random quote generator to your site.

Method 1: Use “Server Side Includes”. Not all webhosts have this setup, but if they do, it is an easy way to include CGI scripts (or other files) in your webpages. These are like normal webpages, but they end in “.shtml” and use special syntax to include other files. For our random quote generator, use the following code inside any “.shtml” file:

<!--#exec cgi="/path_to_this_script.cgi" -->

Make sure to set the path to the random quote script or it will not function correctly.

Method 2: Use a PHP file. The random quote generator can also be included in a PHP file. I plan on writing a PHP version soon, but until then, use the following code inside any PHP pages:

<?php print file_get_contents('http://www.domain.com/random-quote.cgi'); ?>

Remember to change the code to point to your copy of the random quote generator or you may experience some weird results.

If all went well, you should now have a random quote generator running on your site. I can't possibly cover all the problems one might encounter when trying to set this up, so if you ran into problems, I would be more than happy to resolve them with you. And if you have suggestions or ideas for improvements (or new scripts), please feel free to send them my way.

Enjoy.

Stumble This Post DiggIt! Del.icio.us Yahoo Reddit Newsvine Technorati

Posted in , Phil's Blog, Website Tips & Tricks |

Comments are closed.

Support PhilStewart.org


BlueHost.com was voted best support in 2004, 2005 & 2006! Offering a free domain, 600GB space, 6TB transfer, SSH, SSL, FTP, Stats, CGI, Ruby, Perl, PHP & MySQL all for just $6.95/monthSign up now!


    Game of the Day

    Fish Hunter 2 - Caveman fishing<br> with spears.<br>(No, not Britney)

    Fish Hunter 2
    Caveman fishing
    with spears.
    (No, not Britney)
    Play

Recent Posts

Categories

Stinky Hobo Stories


    This hilarious paperback book is now available.

Cool Links

Subscribe