PHP-based chat room (I) Source: limodou author: MikeHall translator: limodou previously had a quite popular web chat room called StarTrekker. I can come to this chat room to thank one of my friends, and even if StarTrekker's chat friends are almost different from me, SyntaxHighlight
PHP-based chat room (1)
Source: limodou
By Mike Hall
Translator: limodou
There used to be a very popular web chat room called Star Trekker. I can come to this chat room to thank one of my friends, and even if the chat friends of Star Trekker are almost not in the same circle as me, I find that for most of them, both are friendly and interesting. But when Star Trekker is disabled, it has to thank Perl running in the background. it swallowed up server resources, so these happy and friendly people have to leave and go nowhere. I was lucky to have opened my own imitation Star during that time.
Trekker's chat room, and began to try to contact homeless chat friends of many of the original Trekker. Out of vigilance against Perl's resource consumption problems, I was very happy when a friend recommended PHP to me.
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 advantages are,
Our chat room is smarter than its BeSeen cousin.
Name:
Message:
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.
<? Php
// 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.
<? Php $ new_message = "$ name: $ 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:
<? Php
// 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:
<? Php
$ 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.
<? Php
$ 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.