PHP-based chat room (1)

Source: Internet
Author: User
There used to be a very 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 not in the same circle as me, I find that for most of them, both are friendly and interesting. But when StarTrekker is closed, Thanks for running in a 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 StarTrekker-like chat room during that time and began to try to contact homeless chat friends of many former 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.



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 array contain my header information, and I don't need it. The loop end condition is $ 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.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.