This is a simple chat room program designed with PHP. The goal is to familiarize everyone with how PHP processes form data! This well-designed web chat room applies variables passed from forms and processes them as HTML,
This is a simple chat room program designed with PHP. The goal is to familiarize everyone with how PHP processes form data!
This well-designed web chat room applies 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 benefit 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 elegant, but whatever the purpose, this is what you want to deal. It sends two variables to chat. php3 to distinguish between $ 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 dialogs. 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 into an array.
There are 12 message files. In the 12 rows, the 1st action header information, the old messages from 2nd rows to 11 actions, and the 12th rows contain my script.
What I like most is to get a string that can contain all those old messages.
// Read the file into an array
$ Message_array = file('messages.html ');
// Compile 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 display 10 messages on the screen. Modify the $ counter <10 expression to change the number of contained messages.
Now that old messages are available, I want to create 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, enter the brief header information:
// Except for carriage return characters at the end of the string, carriage return characters cannot be found in other locations. this is very important.
// Put all header information in the same way.
$ Header ='';