Message of the Day in PHP

by Luigi Dragone [e-Mail] [Home Page]

Overview

This is a litte collection of PHP functions to manage a "Message of the Day" feature in a dynamic web page.

It can be useful to implement a UNIX fortune-like welcome message on a web site, but it can also integrated to produce random e-mail signature. The message file, from which the system picks up a random item (called fortune cookie or simply cookie), has the standard format of UNIX fortune file.

There is also available a collection of English fortune cookies to use as a sample database. I have collected these quotes from many sources freely accessible on the Web, since I suppose that they are of Public Domain and freely distributable. If you find copyrighted any material, please notice it to me.

Copyright

This software is released under GNU General Public License. It can be freely used, modified and distributed in conformity with the GNU General Public License. The License Agreement is available here. For any information about the GNU GPL and its implications please refer to GNU site. The License Agreement is provided in the source file.

Notice that cookie collection is provided as Public Domain material.

System Requirements

In order to use this software you need a PHP 4 compatible web server. A PHP module can be downloaded PHP site and it can easily integrated with Apache Web Server. To use PHP with another web server please refer to their own documentation.

Download

The library source file is available here, and the sample cookie database is here.

Instructions

These instructions are assuming that a PHP interpreter is installed and correctly configured and the source file has been downloaded.

Building the message database

The message database is an ASCII text file containing a list of "fortune cookies". Each cookie is represented as set of lines and is delimited by lines starting with two %s.

This is an example of message file:

This is the 1st message
%%
This is the 2nd message (1st line)
This is the 2nd message (2nd line)
%%%%%%%%%%%%%%%%%%%%%%%%%%%
Thi is the 3rd message
To edit the message database you need a simple text editor.

Indexing messages

To speed up the cookie picking step we build an index of the database. This index is a text file containing the start offset of each cookie, thus the item picking procedure is the following:
  1. Read the index in a vector
  2. Randomly pick an element of the vector
  3. Read the message file from the offset specified by the vector element previously selected until the next message delimiter
The index must be rebuilt each time the message database is updated, but this step can be done offline.

Suppose that the database to be indexed is called cookie.txt. In order to index this file the index cookie.ndx we write this simple PHP script:

<?php
	require ('cookie.inc');
	build_index ('cookie.txt', 'cookie.ndx');
?>
to be saved in the cookie.txt directory as build_index.php. We put the file cookie.inc in the same place. The script can be executed with the following command:
php < build_index.php
assuming the PHP script interpreter is correctly added to the PATH enviroment variable (see the PHP installation instructions and the operating system documentation) and the current directory is the one containing the database file and scripts. If everything goes right a file cookie.ndx has been created.

Developing the web page

Each PHP page that shows a message of the day have to require the file cookie.inc. The directive depends on the location of the included file w.r.t. the location of the page, assuming that they are located in the same directory, the following PHP statement should be enough:

	require ('coockie.inc');
It can be modified according to installation specific issues.

To print out a message of the day you need to use the function pick_a_cookie, that takes two arguments:

  1. the name of the message file including the path
  2. the name of the index file including the path
Check your installation path settings to determine the correct ones.

This function builds a <QUOTE> element and delimits each message line with a <BR> tag, it inserts required HTML entities too. If you need to change this behavior you have to modify the function accordingly.

Publishing on the WWW

To publish a cookied-application we need to upload on the web server the following elements:

  1. the message file built in the first step
  2. the index file built in the second step
  3. the page written in the last step
Notice that after each database update the index must be rebuild (and uploaded too).


luigi@luigidragone.com.