fortune.php tutorial

  1. Brief history of fortune
  2. How it works
  3. PHP version
  4. Limitations
  5. Usage
  6. Fortune cookies files available in this website
  7. Back to menu
"; ?>

1.Brief history of fortune

The fortune program has been written under NetBSD, one of the numerous Unix clones. It displays a random quote from one or more files. It has been ported under Linux, then as its source code became more generic, under other platforms, like SunOS 4.x for example.

For more information about fortune's history, do a man fortune on your favorite Un*x system!


2.How it works

Every "bunch" of quotes is split into two files: a text file containing the quotes themselves, separated by a special character, usually %. Since version 1.4, an index file with .dat extension containing pointers to each quote is added to the text file.

In fact, fortune reads a .dat file and take a random reference, wich contains the position of the corresponding quote, and then use this reference to get and display the quote. This speeds up greatly selection from big files (several thousands of quotes)

.dat files are automaticaly generated by the strfile program, provided by the fortune package.

Note that fortune is not just for quotes, you can also use it to display informations, adverts, etc.!


3.PHP version

Original and PHP versions works the same way: randomly take a quote file, then select a quote in the index file, and eventually display the corresponding quote.

Options works also the same way: without parameter, fortune.php selects a random quote from all available files; with a file name in parameter, the quote will be extracted from this file only.

The quote is displayed in raw mode, without any decoration. According to your mood, you can add effects, colors, etc.


4.Limitations

The strfile program used to produce index files has not been ported in PHP. You still have to use the original program for indexing.

Original fortune program provides multiples options, like a specific drawing (like 20% for file quotes1 and 80% for file quotes2), or to display "offending" quotes separatly...None of these options have been ported. In fact only basic functions are supported: random drawing from a specified file, or from all files. I wanted to avoid overloads in the PHP code, and this way get a faster script. But if you think you can improve it, it's up to you!!


5.Usage

Insert the following code in a PHP page to display a random quote on your website:

<?php
include("http://tourmentine.com/modules/fortune.php");
?>


If you provide a file name as a parameter, quotes will be extracted from this file.
Use the following syntax in this case:

<?php
$FileName="Name_of_File";
include("http://tourmentine.com/modules/fortune.php");
?>


You can also use it in simple HTML link:

<a href="http://tourmentine.com/modules/fortune.php?FileName=Pensees">Click here to display the quote of the day</a>


Or via JavaScript (in your start page for example ;). Be careful, the script's name is not the same:

<script language="javascript" src="http://tourmentine.com/scripts/fortune.js"></script>

<script language="javascript" src="http://tourmentine.com/scripts/fortune.js?FileName=tolkien_fr"></script>

6.Fortune cookies files available in this website

Note: All files below are in French, but it's very easy to find fortunes in English or other languages.

Click on a file's filename to display a random quote in a new window, or click on download to download an archive (you can extract it with gzip/tar, WinZip, or PowerArchiver) containing the quotes file and its .dat file. Once extracted, these files can be used by both fortune program and fortune.php script.




(Updated ".date("M j Y",filemtime(__FILE__)).")

"; echo "
"; ?>