PHP-based chat room (1)
This well-designed web chat room uses variables passed from forms, processes them as HTML, and then writes them into files. Put the form and information file in a framework. You can see that it is very similar to a chat room named BeSeen. Of course, its advantage is that our chat room is smarter than its BeSeen cousin.
The above is the basic form for input. You may want to make it more beautiful, but for whatever purpose, this is what you want to deal. It sends two variables to chat. php3, namely $ name and $ message.
However, before processing those variables, We need to extract the current content from the message file. Otherwise, we can only see one message at a time. There is almost no way to manage conversations. Only when I am as familiar with the structure of my own message files, I know that each message ends with a carriage return. This means that you can use the file () function to read the message file to an array.
There are 12 message files. In 12 rows, 1st rows contain the header information, 2nd rows to 11 old messages, and 12th rows contain my script.
What I'm most interested in is getting a string that can contain all those old messages.
// Read the file into an array
$ Message_array = file ("messages.html ");
// Edit the string
For ($ counter = 1; $ counter <10; $ counter ++ ){
$ Old_messages. = $ message_array [$ counter];
}
?>
When processing strings, I initialize $ counter of the for loop to 1 instead of 0. This is because I know that The 0th elements in the $ message_array contain my header information and I don't need it. In addition, by setting the loop end condition to $ counter <10, it means that only the elements 1 to 9 in the array are read into the string. For the remaining two elements, 11th contain my script, and 10th contain the oldest message. I want to delete both of them, because at any time I only want 10 messages to be displayed on the screen. You can modify the $ counter <10 expression to change the number of contained messages.
Now that old messages are available, I want to generate new messages. We already have two variables: $ name and $ message, so it is easy to write a new message.
";?>
We are about to write a good news file. The rest is the header information and the script. First, add a simple header information:
// Except for carriage return at the end of the string, carriage return is not allowed in other places, which is very important.
// Put all header information in the same way.
$ Header ="";
?>
We want the message screen to be automatically refreshed so that people can see new posts. Instead of using JavaScript, I used META tags for Refresh, mainly because it may be more easily supported by clients. I also don't want the search engine to index my message files. Therefore, the header information is redefined as follows:
$ Header =" ".
" ".
"";
?>
In the script of a file, I usually place a small segment of copyright information, and the ending mark corresponding to the opened header information.
$ Footer ="
".
"©Mike Hall 2000
";
?>
Wrap the copyright information to indicate that the copyright information will be visible only when it is selected, because its color is the same as that of the background color #000000. This is only intended to prevent the display from being affected.