PHPBuilder - PHP-Based Chat Room



RSS Twitter
Articles Html

PHP-Based Chat Room

by: Mike Hall
|
July 30, 2000

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"
?>

1
|
2
|
3
|
4
|
5
|
6
Next Page »

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Mike Hall

Comment:



Comment:

(Maximum characters: 1200). You have characters left.