[Front page] [HTTPget] [AdBlock] [PHP Fortune] [GPS] [Mandelbrot] [XSL tutorial] [C tutorial] [TiddlyWiki] [Contact]

"What is the PHP-fortune script?"

The Fortune-cookie-program has been around the Unix/Linux-world forever. The basic idea was that the user would get a random fortune every time he logged into his terminal. Today this kind of functionality is found in programs like KFortune for KDE.

The program works by keeping a repository of quote-files somewhere on disk. When invoked, the program will look through the list and pick a quote from one of the files. The probability that a file will be picked is proportional to the number of quotes in it.
A collection of quotes consists of two files: a .dat-file containing a binary index into the other file, which contain all the quotes, seperated by %.

22nd of November 05 - Dat-file creator

Alexandre Passant has created Fortune-packager, which makes it very easy to create your own fortune-collections for use with this script.

4th of May 03 - Tiny update

Updated the homepage with better instructions and a complete, working example.

14th of June 02 - Version 1.1

Features added:

  • Added the createIndexFile - function which allows users to create their own fortune-files.
  • Wrapped all functions in a class.

"Why should I use this script, and not any of the other quote scripts available?"

The best reason for using this script is that it's based on an already existing and widely implemented standard.

It's possible to find several thousand quotes, all free and very easy to use with this script, and thus you won't have to find and type in all the quotes from scratch. Browse through Freshmeat's collection to see what's available.

Furthermore, I've kept the script's use of resources as low as possible. The script doesn't read entire text-files to memory, just to pick one line and print it, which I've noticed most other non-database quotescripts do. Only the necessary information is read from disc.

A database is not required to use this script.

"Can I see an example?"

Sure! :) The following text is a quote from one of the fortune-files.

I'll say it again for the logic impaired.
-- Larry Wall

The current fortune files include (among others) quotes from Discworld, Hitchhikers Guide to the Galaxy, Calvin & Hobbes, Futurama and Simpsons.
Reload this page to get a new quote.

"Where can I get this script and how do I use it?"

You can download version 1.1 here: fortune11.zip.

You use it by including the file fortune.php in your own script. Make an instance of the object Fortune.

 $f = new Fortune; 
After this call either
with the location of a directory containing fortune-files or
with the location of a specific .dat-file. Both of these functions return a string, which it's up to you to print and format in HTML.

A complete working example is:
$f = new Fortune;
echo $f->quoteFromDir("dat/");
Where "dat/" is the directory in which the fortune-files are kept.

Making new fortune-files
To make your own fortune-files for use with this script, use the function

$f->createIndexFile($file, [$verbose])
This takes the name of a text-file with quotes as an input. The second argument determines whether the function will print a bit of information about the analysis of the file. The argument is a boolean. Use true to see detailed information after the creation of the file.
The input file must be a text-file with all the quotes, seperated by % on a line by itself. Take a short look at a standard fortune-file to see the format.

This function was added because I realized that it's not certain that people have access to the original strfile-utility for making the fortune-files. createIndexFile will probably not be part of any standard-page you'll make with the script. Make sure that the user running the webserver will have write-access to the directory in which the script executes.

This script was written by Henrik Aasted Sorensen, haas@itu.dk.
Feel free to mail comments and/or suggestions. I would also like to hear from you if you actually start using this script on your page. :)

Parse error: syntax error, unexpected $end in /home/aastedorg/bbclone/var/access.php on line 97