Once upon a time there was a reasonably popular web-based chat room called Star Trekker chat. I happened into this chat thanks to a friend and even though Star Trek fans were hardly my favourite group of people I found that for the most part people in there were friendly and fun. But when Star Trekker shut down, thanks to its Perl backend eating server resources for lunch, these happy and kindly people were left with nowhere to go. It was fortunate that at that time I opened my own similar chat room and managed to attract much of the homeless traffic from Trekker. Wary of the resource problems caused by Perl, I was pleased when a friend introduced me to PHP.
This particular design of web-based chat uses variables posted from a form, processes them into HTML and writes them to a file. Put the form and the message file in a frameset and you have something that looks reasonably like a BeSeen chat room. Of course the advantage is, our chat room can be a little more clever than it's BeSeen cousin.
<form action="chat.php3" method="post">
Name : <input type="text" name="name"><br>
Message : <input type="text" name="message"><br>
<input type="submit" value="Send">
</form>
This is your basic form input. You'll probably want to pretty it up more than that, but to all intents and purposes, this is that you're dealing with. It sends two variables through to chat.php3 called $name and $message.
Before we deal with those variables, however, we need to extract the current contents of the message file, otherwise we'd only see one message at a time. Hardly a way to conduct a conversation. Being familiar as I am with the structure of my own message file, I know that each message is terminated by a newline character. This means I can use the file() function to read the message file into an array.
The message file is 12 lines long. Of those 12 lines, the line 1 is a set of headers, lines 2-11 are old messages and line 12 contains my footers.
All I am interested in is obtaining a string that contains most of those old messages.
<?php
// Read file into an array
$message_array = file("messages.html");
// Compile the string
for ($counter = 1; $counter < 10; $counter++) {
$old_messages .= $message_array[$counter];
}
?>
When compiling the string, I initiated the for loop with $counter = 1 not $counter = 0 as is common. This is because I know that element 0 of $message_array contains my headers and I don't want those. Also, by setting the loop condition to $counter < 10 means that only elements 1 thru 9 of the array are read into the string. Of the other two elements, 11 contains my footers and 10 contains the oldest message. Both of which I want to remove so I only ever have 10 messages on screen at any given time. Altering the $counter < 10 expression allows you to vary the amount of messages retained.
Now I have my old messages I want to make the new message. We have our two variables $name and $message so writing a new message string is easy.
<?php
$new_message = "$name : $message<br>\n";
?>
We're nearly ready write our message file. All we need are headers and footers. Start simple with the headers:
<?php
// It's important that there are no newline
// characters except at the end of the string.
// This keeps all the headers together.
$header = "<html><body bgcolor=\"#000000\" text=\"#ffffff\">\n";
?>
We want the message screen to auto refresh so people viewing the site can see new posts. In preference to using JavaScript, I use an META refresh, principally because it's more likely to be supported client-side. I also don't want the search engines indexing my message file. So we refine $header to :
<?php
$header = "<html><head><meta http-equiv=\"refresh\" content=\"8\">".
"<meta name=\"robots\" content=\"noindex\"></head>".
"<body bgcolor=\"#000000\" text=\"#ffffff\">\n";
?>
In the file footer I tend to put a little copyright information as well as close the tags I opened in the header.
<?php
$footer = "<p align=\"center\"><font color=\"#000000\">".
"© Mike Hall 2000</font></p></body></html>";
?>
Wrapping the copyright in <font color="#000000"> means that unless selected it'll be invisible against the equally #000000 background. This just stops it being intrusive.

