random quote CGI script

There are a million variations of this on the Internet. The benefit of this one is that it works with the same file format as that used by signature, an e-mail signature randomizer (which works great with mutt and Evolution.)

CGI script: standalone or include through SSI


$quotefile_path="path to the file on your server containing your quotes";

open (QUOTEFILE,$quotefile_path);
while (<quotefile>) {
         if ($_ eq "~\n") {
         else {
                  # Escapes all quote characters in case you intend to use the
                  # Javascript client-side kludge....otherwise you don't really
                  # need it
                  @quote[$count]=@quote[$count] . $_;

@output=split /\n/, @quote[int rand $count];

print "Content-type: text/html\n";
print "\n";


# Put whatever else you want to output here...this is likely to be opening markup.
# example:
# <html>
# <body>



for (@output) {
    print "@output[$line] ";

# Close the tags you put in the OPENING section...
# example:
# </body>
# </html>


CGI script: use Javascript client-side include kludge

It's basically the same thing, except you have to change the HTTP header from

print "Content-type: text/html\n";
print "\n";


print "Content-type: text/javascript\n";
print "\n";

You will also have to wrap all your tags in the OPENING and CLOSING sections with document.write methods

For example,




You will also have to change the print statement in the last for loop from

print "@output[$line] ";


print "document.write(\"@output[$line]\"+\" \")\n";

Then invoke the CGI script this way... (Stick the following in your HTML):

<script language="Javascript" src="URL of CGI script">

quote file format

Quote #1


Quote #2


Quote #3


The tildes (~) must each be on a line by themselves in order for the CGI script (and for signature) to parse it properly. The final tilde is not strictly necessary for the script, but seems to be necessary for signature.

The script would only need a minor modification in order to parse fortune files.

Created on 2001-04-24
code main | fato profugus