PHP Chat Room Technology

Source: Internet
Author: User
Tags exit flush functions html form mysql mysql database telnet program
PHP Chat Room Technology
Huang

1. Objective
Chatting online is the most popular way to make friends nowadays. Each big website launches the chat room to have each unique characteristic.
Chat rooms are mainly divided into webchat, bbschat two kinds. Bbschat is a TCP protocol based on Telnet, which is an attached function of BBS, and needs the client Telnet program. Webchat is a browser, which is actually a CGI program that many people use together. The basic principle is that each user's speech through the browser to the system, and then collected and processed by the system 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 the server push is a multiple MIME encoding of the data to the (push) user side, which is currently less used by the web. The client pull is the data that the user wants from the server pull (pull).
The most commonly used client pull is to use the HTML META tag http-equiv= the "Refresh" attribute to check for new data on the server at regular intervals. For example , the page is refreshed every 5 seconds. This method is simple and effective, the disadvantage is that the flash will produce flicker, and in order to maintain efficiency, each refresh will be the old chat content purged, users want to view or retain the content of the conversation is very inconvenient. To this end, the use of Javaapplet as the front of the chat room, the use of refreshing data from the server pull through the Javaapplet to display is also a solution. There is also the introduction of this article to enable chat programs to maintain the connection. Method one, the chat program is set to infinity, can make the browser keep the download of the line state; method Two, there is an infinite loop in the chat program, because it can easily set more advanced functions, so choose to use here.
The Web server uses Freebsd+apache because the combination of the two has the strongest performance and costs zero. What you need to think about 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 efficiency, and FreeBSD IO performance is poor. Consider using RAMDisk to put the entire file into memory to improve speed. Or a high speed space is divided into memory for data storage. The author uses the database way: MySQL. Because the database is designed for high-volume users at the same time, using it can eliminate the design of high-speed space operations 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 operates primarily on the various elements of the HTML form. JavaScript is an object-based language, the various elements in HTML as objects, so each element of the method and attributes are very rich, easy to operate. and PHP only after the form after the post, the form of the elements into the corresponding variables to process the user input data. It's bad for interactivity, and that's why you use JavaScript. The reason for using PHP is that it is faster and more secure than other CGI languages, and easy to develop.
2. Constantly refreshed chat room
A standard Chat room page consists of three frame, which is a list showing online users, say of user statements and feature settings, and display of chat content. Users in the say frame typed the speech, press send, the data after processing to save in the MySQL database, but also saved include speakers, chat objects and time to speak. As the user enters the chat room, the List frame displays the statements from the MySQL database that speak longer than the user's entry time. The key to the subsequent display of the new speech content is that the program that shows the chat content is infinitely cyclic.
List Frame Program Overview:
$db =mysql_pconnect (localhost,root); #Mysql数据库连接
mysql_select_db (chat, $db);
Show welcome into chat room
Set $init to speak time in the database
The ID number of the first data that entered the big time #是数据提取的标志位
while (1==1) {#无限循环开始
Extracting data from $init to the final;
while (each data $text) {
$emote =strip_tags ($text); #防止用户直接输入Html语言
if (eregi ("^/", $emote)) #判断发言是否系统命令 (with/beginning)
System Special Handling
Else Show speech
}
Set the ID number of the $init as the last data
Flush (); #清出输出缓冲 to make the speech immediately show
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 first into the output buffer. by Flush () immediately the contents of the buffer to the user's list frame sent out, to achieve real-time chat effect. The loop finally releases the memory occupied by the MySQL result set, otherwise the system resources will soon be depleted due to infinite loops.
After login, the user will establish a online table for the on-line user's statistics, mainly in order to prevent the user table is too large, the program frequently used in the search will slow down the operation of the system. The most used is the online Frame, which uses the client pull refresh Meta to query the online table every once in a while to refresh the users on the line. If the user does not speak more than the specified time, the system invokes the custom function to set the user to timeout and force it to exit the chat room.
3. Introduction to User function
The user's function is set in the say frame, you can choose the speech map, the tone of the speech and so on. After special processing of the speech stored to MySQL, such as the selection of a map, the system will be added to the front of the speech, you can achieve the effect of the map.
For chat room commonly used emote, for example user a input "/hello" press sends, the List frame displays is "the user a happily and everybody greets", in order to maintain the efficiency, the user input emote the original text directly saves to the database, but the analytic transformation work by the list Frame to complete.
Whisper only yourself and the chat object can be seen, implemented because the speaker and chat object are saved beforehand, just make a simple judgment. There are often chat rooms to screen the function of a user to speak, by setting a temporary array to achieve, there is no need to save in the user's database.
In the chat room when the person is many people, the person who rushed to speak is often dazzling, at this time can choose whether to shield irrelevant speech, that means that as long as not to everyone and their speeches will not be shown. Of course, because the system will use a special color to identify and talk about their own, even if you do not choose this feature, users will be able to quickly from a number of speeches to find their own relevant.
Users can pack their speeches about the same day at a specific time (when the system is more idle). Because the data stored on the table is growing fast, the system will copy it and empty it the next day to keep it running efficiently. This allows users to retrieve packaged speech content will not affect the operation of the chat system.
For security reasons, the chat room management function is independent, and is not placed in the chat page. Mainly has the user data management and will disrupt the user kick out (kicking person) two kinds of 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.
Chat room security Requirements although not as high as e-commerce, but if the user is an imposter, the user's speech is eavesdropping, or disorderly kicking people, is very unpleasant. Users must fill in login name and password in order to enter the chat room, but by viewing login source code, although can not see the part of PHP, HTNL part of the source is to show login is called chat.php program to enter the chat room. So in order to prevent users from entering the chat room, the system will first determine whether the newly established chat room is generated by login, not exit. Similarly, the list frame and post frame in the chat room can also be added to this protection. Of course, checking the user's identity and password in the list frame and post frame is foolproof, just adding to the burden of the system.
To sum up, can see the system's source code is undoubtedly a dangerous beginning, so the system to write as far as possible to see the source code PHP; set up by login opened chat room Hidden browser toolbar, the status bar, and shielding the right mouse button and shortcut keys.
4. Summary
The chat room written in PHP has the characteristic of high efficiency and stability, and it is the best choice to write the network interactive program.
Reference documents:
[1] Rasmus Lerdorf. PHP Manual [M]. Electronic documents, 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.