PHP Chat Room Technology

Source: Internet
Author: User
Tags telnet program

PHP chat room technology Huang Guohui

1. Preface

Online chat is the most popular way to make friends. Chat rooms launched by various websites have their own characteristics.

Chat rooms are divided into WebChat and BBSChat. BBSChat is a Telnet-based Tcp protocol. It is a function attached to BBS and requires a client Telnet program. WebChat adopts a browser, which is actually a CGI program used by many people. The basic principle is to send each user's speech to the system through a browser, and then the system collects and processes the speech and delivers it to specific users.

WebChat generally uses Server Push or Client Pull technology. The difference between the two technologies is that different methods are used to distribute data to users. Server Push means that the Server uses multi-MIME encoding to push data to (Push) users, currently, few websites use this method. The Client Pull is the data that the user pulls from the server (pull.

The most common Client Pull is to use the Meta tag http-equiv = "Refresh" in the Html language to check whether new data exists on the server at regular intervals. For example, the page is refreshed every five seconds. This method is simple and effective. The disadvantage is that it will flash during refresh. In order to maintain efficiency, the old chat content will be cleared every time you refresh the page, it is inconvenient for users to view or retain the conversation content. Therefore, using JavaApplet as the front-end of the chat room, and using refresh to display Pull data from the server through JavaApplet is also a solution. There is also the scheme to keep the chat program connected. Method 1: Set the chat program to be infinitely large, so that the browser can keep the download link continuously. Method 2: There is an infinite loop in the chat program, because you can easily set more advanced features, you can choose to use them here.

The Web server uses FreeBSD + Apache because the combination of FreeBSD and Apache has the strongest performance, and the cost is zero. You need to consider how to store data. It is relatively easy to implement with files, but it is often used by many people to perform IO operations on the same file, which will inevitably affect the efficiency. Moreover, FreeBSD's IO performance is not good. You can use RamDisk to put the entire file into the memory to increase the speed. Or you can allocate a high-speed space in the memory for data storage. The author uses the database method: MySQL. Because the database is designed for the simultaneous use of a large number of users, it can save the complexity of designing high-speed space operations and ensure the speed. If the entire database is put into the memory, the effect is better.

The program is written in PHP + Html + JavaScript. The chat room is mainly used to operate various elements in the Html Form. JavaScript is an object-based language that treats all elements in Html as objects. Therefore, each element has rich methods and attributes and is easy to operate. PHP can process user input data only after the Form is Post and the elements in the Form are converted into corresponding variables. Poor interactivity, which is also the reason for using JavaScript. The reason for using PHP is that it is faster and more secure than other CGI languages, and it is easier to develop.

2. Refresh chat rooms

A standard chat room page consists of three frames, which display the Online user's Online, the user's speech, the function setting's Say, and the List of chat content. The user typed in the speech content in the Say Frame and sent it. The data was processed and stored in the Mysql database. At the same time, the Speaker, chat object, and time of the speech were saved. As soon as the user enters the chat room, the List Frame will display the comments whose speaking time is later than the user's entering time from the Mysql database. The key to displaying new speech content in the future is that the program that displays the chat content is infinite loop.
Summary of the List Frame program:
Copy codeThe Code is as follows:
$ Db = mysql_pconnect (localhost, root); # Mysql database connection
Mysql_select_db (chat, $ db );
Show welcome to chat room
Set $ init to the database speaking time ratio
Enter the ID of the first data with a large time # It is the identifier of data extraction
While (1 = 1) {# Start of an infinite loop
Extract data from $ init to the end;
While ($ text for each data ){
$ Emote = strip_tags ($ text); # Prevent Users From directly entering the Html language
If (eregi ("^/", $ emote) # judge whether the speech is a system command (starts)
Special System Processing
Else presentation
}
Set $ init to the ID of the last data
Flush (); # Clear the output buffer so that the speech is displayed immediately
Sleep (2); # Pause the program for 2 seconds to save system resources
Mysql_free_result ($ result); # Release the memory occupied by the database results
}


Because the program has infinite loops, the output speech in each loop is first placed in the output buffer. Uses flush () to immediately send the buffer content to the user's List Frame, achieving real-time chat. At the end of the loop, the memory occupied by the Mysql result set will be released. Otherwise, system resources will soon be exhausted due to the infinite loop.
After Login, you can create an Online table for Online User statistics. The main purpose is to prevent the User table from being too large. The frequently used searches in the program will slow down the system operation. Among them, Online Frame is the most used. When Client Pull's Refresh Meta is used, Online tables are queried at intervals to Refresh Online users. If the user does not speak for more than the specified time, the system will call the custom function to set the user to TimeOut and force the user to exit the chat room.

3. User features

The user's function is set in the Say Frame. You can select the texture and tone of the speech. After special processing, the speech is stored in Mysql. For example, after a texture is selected, the system adds the texture to the front of the speech to achieve the effect of the texture.
For Emote commonly used in chat rooms, for example, if user A enters "/hello" and sends it, "user A happily greets everyone" is displayed in the List Frame. To maintain efficiency, the Emote entered by the user is saved to the database directly, and the parsing and conversion work is completed by List Frame.
Only the speaker and chat object can be seen in the whisper. The speaker and chat object are saved in advance, so you only need to make a simple judgment. There is also a common function of blocking a user's speech in chat rooms, which is achieved by setting a temporary array, and there is no need to store it in the user's database.
When there are many people in the chat room, it is often dazzling to compete for speeches. You can choose whether to block irrelevant speeches, that is, as long as they are not for everyone and their own speeches will not be displayed. Of course, because the system uses special colors to mark the speeches related to itself, even if this function is not selected, the user can quickly find their own problems from the numerous speeches.
You can package your own speeches at a specific time (when the system is idle. Because the data stored in the table is growing fast, the system will copy the table and clear it the next day to maintain operational efficiency. In this way, the user will not affect the operation of the chat system when searching and packaging the speech content.
For the sake of security, the management function of the chat room is independent and is not placed on the chat page. There are two main functions: User Data Management and Kick Out. After a user is kicked out of a chat room, the user cannot enter the chat room for a certain period of time.
Although the security requirements of chat rooms are not as high as those of e-commerce, it is very unpleasant if users are impersonated, their speeches are eavesdropped, or they are kicked out. Users must enter the name and password in Login before entering the Chat room. However, although the source code of Login cannot be viewed in Php, the source code of Htnl shows that Login calls Chat. php program to enter the chat room. To prevent users from entering the chat room directly, the system first checks whether the new chat room is generated by Login. If not, the system exits. Similarly, this protection can be added to List Frame and Post Frame in the chat room. Of course, you can check the user's identity and password in List Frame and Post Frame, which only increases the burden on the system.
To sum up, we can see that the source code of the system is undoubtedly a dangerous start, so the system should try to use Php without the source code; the chat room opened by Login hides the toolbar and status bar of the browser, and shields the right-click and shortcut keys of the mouse.

4. Summary

Chatting rooms written in Php are efficient and stable, and are the best choice for compiling network interaction programs.

References:
[1] Rasmus Lerdorf by. PHP Manual [M]. E-documentation, 2000


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.