PHP Chat Room Technology _php Tutorial

Source: Internet
Author: User
PHP chat Room Technology Wong Kwok Fai

1. Objective

Chatting online is the most popular way to make friends nowadays. The chat rooms are featured on each major website.

Chat rooms are mainly divided into webchat, bbschat two kinds. Bbschat is a Telnet-based TCP protocol that is attached to the BBS and requires a client Telnet program. Webchat is a browser-based, in fact, a multi-person common use of CGI programs. The basic principle is to send each user's speech through the browser to the system, and then the system is collected and processed and distributed to specific users.

Webchat generally uses server push or client pull technology. The difference between the two technologies is that the data is distributed to the user in different ways, and server push is used by the server to encode the data in multiple MIME encodings to the (push) consumer side, which is currently less available to the Web site. The client pull is the data that the user pulls from the server.

The most commonly used client pull is to use the HTML META tag to http-equiv= the "Refresh" attribute, checking the server for new data every once in a while. For example, the page will be refreshed every 5 seconds. This method is simple and effective, the drawback is that flashing occurs when the refresh, and in order to maintain efficiency, each refresh will be the old chat content, users want to view or retain the conversation is inconvenient. To this end, using Javaapplet as the front end of the chat room, using the refresh to pull the data from the server through the Javaapplet to display is also a solution. There is a scenario that this article describes to keep the chat program connected. Method one, the chat program is set to infinity, you can make the browser keep the download of the connection status; Method Two, there is an infinite loop in the chat program, because you can easily set more advanced features, so choose to use this.

The Web server uses Freebsd+apache because the combination of the two has the strongest performance and it costs zero. What else to consider is how to store the data. The file is relatively easy to implement, but many people use, frequently to the same file IO operation, will inevitably affect the efficiency, and FreeBSD IO performance is poor. Consider using RAMDisk to put the entire file into memory to increase speed. Or a piece of high-speed space in memory for data storage. I am using the database method: MySQL. Because the database is designed for high-volume users at the same time, using it can save the design of high-speed space operation of the complex writing, speed can be guaranteed. If you put the entire database into memory, the effect is better.

The program is written in Php+html+javascript. The chat room is primarily about the various elements in the HTML form. JavaScript is an object-based language that treats all elements of HTML as objects, so the methods and properties of each element are rich and easy to operate. PHP can only process user-entered data by turning the elements in the form into corresponding variables after the form has been post. This is a poor interaction, which is why you use JavaScript. The reason for using PHP is that it is faster and more secure than other CGI languages and is easier to develop.

2. A constantly refreshed chat room

A standard Chat room page consists of three frames, which are the say of online users, user statements and feature settings, and a list of chat content displayed. The user presses the content in the say frame to send, the data is processed and saved in the MySQL database, and the time of the speaker, the chat object and the speaker is saved. When the user enters the chat room, the List frame displays the statement from the MySQL database that the speaking time is greater than the user's entry time. The key to the subsequent display of the new statement is that the program that displays the chat content is an infinite loop.
Overview of the List frame program:
Copy CodeThe code is as follows:
$db =mysql_pconnect (localhost,root); #Mysql数据库连接
mysql_select_db (chat, $db);
Show Welcome to the chat room
Set $init to speak time in the database than
The ID number of the first data entering the time is large #是数据提取的标志位
while (1==1) {#无限循环开始
Extracting data from $init to the last;
while (each data $text) {
$emote =strip_tags ($text); #防止用户直接输入Html语言
if (eregi ("^/", $emote)) #判断发言是否系统命令 (with/start)
System Special Handling
Else Display speech
}
Set the ID number of the $init as the last data
Flush (); #清出输出缓冲, so that the speech immediately showed
Sleep (2); #使程序暂停2秒, Save system resources
Mysql_free_result ($result); #释放数据库结果占用的内存
}


Because of the infinite loop of the program, the statements that are output in each loop are placed in the output buffer first. The content of the buffer is sent to the user's list frame by flush (), which achieves the real-time chatting effect. The loop finally releases the memory occupied by the MySQL result set, or the system resources will be exhausted soon because of the infinite loop.
After login, the user will establish a online table for the statistics of users on-line, mainly to prevent the user table is too large, the program frequently used in the retrieval will slow down the operation of the system. One of the most used is the online Frame, which uses the refresh Meta of client pull to query the online table every once in a while to refresh the users on the line. If the user does not speak longer than the specified time, the system calls the custom function to set the user to timeout, forcing it to exit the chat room.

3. Introduction to User functions

User's function is set in say frame, can choose to speak the map, the tone of speech and so on. Speeches are stored in MySQL after special processing, for example, after selecting a map, the system will be added to the front of the speech, you can achieve the effect of the map.
For the emote commonly used in chat rooms, such as User a input "/hello" by the Send, the list frame shows "User A happy to greet everyone", in order to maintain efficiency, the user input emote will be saved directly to the database, and the work of the resolution transformation by the List Frame to complete.
Whisper only oneself and the chat object can see, implementation on because the speech pre-save have speakers and chat object, just make a simple judgment can. There is a chat room often blocking the ability of a user to speak, by setting a temporary array to implement, there is no need to save in the user's database.
Chat room When many people, everyone rushed to speak often dazzling, when you can choose whether or not to block irrelevant speeches, that means as long as not to all people and their speeches will not show up. Of course, because the system will use a special color to identify with their own speech, even if you do not select the function, users can quickly from a number of statements to find themselves related.
Users can package their own speeches for the day at a specific time (when the system is idle). Because the table data that stores the statements is growing fast, the system will copy and empty it the next day to keep it running efficiently. This allows users to retrieve packaged statements without affecting the operation of the chat system.
For security reasons, the chat room management function is independent, and is not placed on the chat page. The main user data management and will be disruptive users kick out (kicking people) two functions. Kicking the user out of the chat room will make it impossible for him to enter the chat room for a certain amount of time.
The security requirements of the chat room are not as high as e-commerce, but it is very unpleasant if the user is hacked by the imposter, the user's speech, or is kicking people. Users have to fill in login name and password to enter the chat room, but by looking at the source code of login, although can not see the part of PHP, HTNL part of the source shows that login is called chat.php program to enter the chat room. Therefore, in order to prevent users from directly entering the chat room, the system will first determine whether the newly created chat room is generated by login, not the exit. Similarly, this protection can be added to the list frame and post frame in the chat room. Of course, checking the user's identity and password in the list frame and post frame is foolproof, only adding to the burden on the system.
To sum up, can see the source code of the system is undoubtedly a dangerous beginning, so the system to write as far as possible to see the source of PHP; Set the chat room opened by login hides the browser toolbar, status bar, etc., and shields the mouse right and shortcut keys.

4. Summary

The chat room written in PHP is highly efficient and stable, and is the best choice for writing network interaction programs.

Reference documents:
[1] Rasmus Lerdorf. PHP Manual [M]. Electronic documents, 2000


http://www.bkjia.com/PHPjc/316554.html www.bkjia.com true http://www.bkjia.com/PHPjc/316554.html techarticle PHP chat Room Technology Wong Kwok Fai 1. Introduction Chatting online is the most popular way to make friends nowadays. The chat rooms are featured on each major website. Chat rooms are mainly divided into webchat, bbschat two kinds. ...

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