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
#!/usr/local/bin/perl
$quotefile_path="path to the file on your server containing your quotes";
$count=0;
open (QUOTEFILE,$quotefile_path);
while (<quotefile>) {
if ($_ eq "~\n") {
$count++;
}
else {
s/\"/"\;/g;
# 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";
print <<END_OF_OPENING;
# Put whatever else you want to output here...this
is likely to be opening markup.
# example:
# <html>
# <body>
END_OF_OPENING
$line=0;
for (@output) {
print "@output[$line] ";
$line++;
print <<END_OF_CLOSING;
# Close the tags you put in the OPENING
section...
# example:
# </body>
# </html>
END_OF_CLOSING
It's basically the same thing, except you have to change the
HTTP header from
print "Content-type: text/html\n";
print "\n";
to
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,
<html>
<body>
becomes
document.write('<html>');
document.write('<body>');
You will also have to change the print statement in
the last for loop from
print "@output[$line] ";
to
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">
</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 |