PHP-based chat room (i) _php tutorial

Source: Internet
Author: User
PHP-based chat room (i)
Source: Limodou
Author: Mike Hall
Translator: Limodou
There used to be a rather popular web chat room called star Trekker chat room. I was able to come to this chat room to thank a friend of mine, and even though star Trekker's friends were almost not in the same circle as me, I found it was friendly and interesting for most of them. But when star Trekker shut down, it had to thank Perl, which was running in the background, to swallow the resources of the server, so the happy and friendly people had to leave and nowhere to go. I had the pleasure of opening my own imitation star during that period.
Trekker's chat room and began trying to connect with many of the old Trekker's homeless friends. Being wary of the problem of resource consumption caused by Perl, I was very happy when a friend suggested PHP to me.
This well-designed web chat room uses variables passed from table only son, processes them as HTML, and writes them to the file. Put the form and information file in a frame, you can see it is like a chat room called Beseen. Of course, the advantage of it is that
Our chat room is smarter than its beseen cousins.
Name:
Message:
Above is the basic form for input. You might want to make it look more beautiful, but for whatever purpose, this is what you're dealing with. It sends two variables to CHAT.PHP3, called $name and $message, respectively.
However, before dealing with those variables, we need to remove the current content from the message file, otherwise we can only see one message at a time. There is hardly a way to manage conversations. Only as I am familiar with the structure of my own message files, I know that every message ends with a carriage return. This means that you can use the file () function to read a message file into an array.
The message file has 12 rows. In 12 rows, the 1th behavior header information, line 2nd to 11 behaves old messages, and line 12th contains my foot markers.
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 String
for ($counter = 1; $counter < $counter + +) {
$old _messages. = $message _array[$counter];
}
? >
When working with strings, I initialize the $counter for the For loop to 1 instead of 0. This is because I know that the No. 0 element of the $message_array array contains my header information and I don't need it. Also, by setting the loop end condition to $counter < 10, it means that only 1 to 9 of the elements in the array are read into the string. For the remaining two elements, the 11th one contains my feet, and the 10th contains the oldest messages. Both of these I want to delete because at any moment I only let the screen show 10 messages. Modifying the $counter < 10 expression allows you to change the number of messages contained.
Now that we have the old message, I want to generate a new message. We've got two variables: $name and $message, so it's easy to write a new message.
< php $new _message = "$name: $message";? >
We are about to write the good news file. All that is needed is the head information and the foot tag. Add a simple header message first:
< PHP
It is important to have a carriage return in addition to the end of the string.
To put all the header information on the same.
$header = "";
? >
We want the message screen to refresh automatically, so people can see the new posts. Instead of using JavaScript, I refreshed it with a meta tag, mainly because it might be more easily supported by the client. I also don't want the search engine to index my message files. So redefine the header information as:
< PHP
$header = "".
"".
" ";
? >
At the foot of the file, I usually place a small piece of copyright information, as well as the end tag corresponding to the open header information.
< PHP
$footer = "".
"©mike Hall 2000";
? >
Wrapping the copyright information in a package means that it will be seen only if it is selected because its color is the same as the background color #000000. The purpose of this is simply to not let it affect the display.

http://www.bkjia.com/PHPjc/532275.html www.bkjia.com true http://www.bkjia.com/PHPjc/532275.html techarticle php-based chat room (i) Source: Limodou Author: Mike Hall Translator: Limodou once had a rather popular web chat room called star Trekker chat room. I can come to this chat ...

  • 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.