Chat room based on PHP (i)

Source: Internet
Author: User
Once upon a time there was a fairly popular web chat room called the star Trekker chat room. I was able to come to this chat room to thank one of my friends, and even though star Trekker's chat friends were almost not in the same circle as me, I found that for most of them, they were friendly and interesting. But when star Trekker closed, it had to thank the Perl running in the background, which swallowed the resources of the server, so the happy and friendly people had to leave and nowhere to go. I had the privilege of opening my own copy of star in that time.
Trekker's chat room and began to try to get in touch with many of the original Trekker's homeless chat friends. Wary of the resource consumption problems caused by Perl, I was very happy when a friend recommended PHP to me.
This well-designed web chat room uses variables that are passed from the table conveys and processes them as HTML and then writes them to the file. Put the form and the information file in a frame, and you can see it is like a chat room called Beseen. The good thing about it is, of course,
Our chat room is smarter than its beseen cousins.
<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>
The above is the basic form for input. You may want to make it a little more beautiful, but for whatever purpose, this is what you have to deal with. It sends two variables to CHAT.PHP3, respectively called $name and $message.
However, before dealing with those variables, we need to remove the current contents from the message file, otherwise we will see only one message at a time. There is hardly a way to manage conversations. Just as I am familiar with the structure of my own message file, 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 lines. In line 12, the 1th Act header information, line 2nd to 11 acts the old message, and the 12th line contains my foot mark.
What I am 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 dealing 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 the loop end condition is $counter < 10, meaning that only elements 1 to 9 of the array are read into the string. For the remaining two elements, the 11th contains my pin, and the 10th contains the oldest message. Both of these I want to delete, because at any moment I only let the screen display 10 messages. Modifying the $counter < 10 expression allows you to change the number of messages contained.

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.