PHP Chat Room Technology_PHP Tutorial

WBOY
Release: 2016-07-21 16:02:32
Original
1677 people have browsed it

PHP chat room technology Huang Guohui

1. Preface

Chatting online is the most popular way to make friends nowadays. The chat rooms launched by major websites have their own characteristics.

Chat rooms are mainly divided into two types: WebChat and BBSChat. BBSChat is a Tcp protocol based on Telnet. It is an additional function of BBS and requires a client Telnet program. WebChat uses a browser and is actually a CGI program used by multiple people. The basic principle is to transmit each user's speech to the system through the browser, and then the system collects and processes it and distributes it to specific users.

WebChat generally uses Server Push or Client Pull technology. The difference between the two technologies is that they use different methods to distribute data to users. Server Push uses multiple MIME encodings of the data and pushes it to the user. Currently, few websites use this method. Client Pull is when the user pulls the data they want from the server.

The most commonly used Client Pull is to use the http-equiv="Refresh" attribute of the Meta tag in the Html language to check whether there is new data on the server every once in a while. For example, the page will be refreshed every 5 seconds. This method is simple and effective, but the disadvantage is that it will cause flickering when refreshing; and in order to maintain efficiency, old chat content will be cleared every time it is refreshed, making it inconvenient for users to view or retain the conversation content. For this reason, it is also a solution to use JavaApplet as the front end of the chat room and use refresh to display the data pulled from the server through JavaApplet. There is also a solution to keep the chat program connected that this article will introduce. Method one, set the chat program to infinite, so that the browser can keep the download connection state; method two, there is an infinite loop in the chat program, because more advanced functions can be easily set, so here Choose to adopt.

The web server uses FreeBSD+Apache because the combination of the two has the strongest performance and costs zero. What also needs to be considered is how to store the data. Using files is relatively easy to implement, but if multiple people use it, frequent IO operations on the same file will inevitably affect efficiency, and FreeBSD's IO performance is poor. You can consider using RamDisk to put the entire file into memory to increase speed. Or allocate a high-speed space in the memory for data storage. The author uses the database method: MySQL. Because this database is designed for simultaneous use by a large number of users, using it can save the need for complex programming of high-speed spatial operations, and the speed can also be guaranteed. If you put the entire database into memory, the effect will be better.

The program is written in PHP+Html+JavaScript. The chat room mainly operates various elements in the Html Form. JavaScript is an object-based language and treats various elements in Html as objects. Therefore, each element has rich methods and attributes and is easy to operate. However, PHP can only process the data input by the user after the Form has been Posted and the elements in the Form are converted into corresponding variables. It is relatively poor in terms of interactivity, which is why JavaScript is used. The reason for using PHP is that compared to other CGI languages, its speed and security are better, and development is easier.

2. Constantly refreshing chat room

A standard chat room page consists of three Frames, which are Online to display online users, Say to display user speech and function settings, and Display chat content. List. The user types in the speech content in the Say Frame and presses send. The data is processed and saved in the Mysql database. At the same time, the speaker, chat partner and the time of the speech are also saved. As soon as the user enters the chat room, the List Frame displays the speeches whose speech time is greater than the user's entry time from the Mysql database. The key to subsequent display of new speech content is that the program that displays the chat content is an infinite loop.
List Frame program summary:

Copy code The code is as follows:

$db=mysql_pconnect(localhost,root); #Mysql database connection
mysql_select_db(chat,$db);
Display welcome to the chat room
Set $init to the ID number of the first data in the database whose speaking time is greater than the
entry time# It is the flag bit for data extraction
while (1==1) { #Infinite loop starts
Extract data from $init to the end;
while (each data $text) {
$emote =strip_tags($text); #Prevent users from directly inputting Html language
if (eregi("^/",$emote)) #Determine whether the speech is a system command (starting with /)
System special processing
else display the speech
}
Set $init to the ID number of the last data
flush(); #Clear the output buffer so that the speech will be displayed immediately
sleep(2); #Pause the program for 2 seconds , save system resources
mysql_free_result($result); #Release the memory occupied by database results
}


Because the program loops infinitely, the statements output in each loop are first placed in the output buffer. The contents of the buffer are immediately sent to the user's List Frame through flush(), achieving a real-time chat effect. At the end of the loop, the memory occupied by the Mysql result set must be released, otherwise system resources will be exhausted quickly due to the infinite loop.
After the user logs in, an Online table will be created for online user statistics. This is mainly to prevent the User table from being too large. Frequently used searches in the program will slow down the operation of the system. The most commonly used one is Online Frame. When using Client Pull's Refresh Meta, the Online table will be queried every once in a while to refresh online users. If the user does not speak for more than the specified time, the system will call a custom function to set the user to TimeOut and force the user to exit the chat room.

3. Introduction to user functions

The user's functions are set in the Say Frame, and you can choose speech stickers, speaking tone, etc. The speech is stored in Mysql after special processing. For example, after selecting a texture, the system will add it to the front of the speech to achieve the effect of the texture.
For Emotes commonly used in chat rooms, for example, when user A enters "/hello" and presses send, what is displayed in the List Frame is "User A happily greets everyone". In order to maintain efficiency, the Emote entered by the user will be directly displayed in the original text. Save to the database, and the parsing and conversion work is completed by List Frame.
Only you and the chat partner can see the private message. In practice, because the speaker and chat partner are pre-saved in the speech, you only need to make a simple judgment. There is also the function of blocking a user's speech that is often found in chat rooms. This is achieved by setting up a temporary array, and there is no need to save it in the user's database.
When there are many people in the chat room, it is often dazzling when everyone rushes to speak first. At this time, you can choose whether to block irrelevant comments, which means that as long as they are not for everyone and your own comments, they will not be displayed. Of course, because the system will use special colors to identify speeches related to them, even if they do not choose this function, users can still quickly find the ones related to them from many speeches.
Users can package the speeches related to them that day at a specific time (when the system is relatively idle). Because the data in the table that stores statements grows very quickly, in order to maintain operating efficiency, the system will copy it and clear it every other day. In this way, the user's retrieval of packaged speech content will not affect the operation of the chat system.
For security reasons, the management function of the chat room is independent and not placed on the chat page. It mainly has two functions: user information management and kicking out troublesome users. Kicking a user out of a chat room will prevent him from entering the chat room again for a certain period of time.
Although the security requirements of chat rooms are not as high as e-commerce, it is very unpleasant if a user is impersonated, a user's speech is eavesdropped, or someone is kicked randomly. Users have to fill in their name and password in Login to enter the chat room. However, by looking at the source code of Login, although the Php part cannot be seen, the source code of the Htnl part shows that Login calls the Chat.php program to enter the chat room. Therefore, in order to prevent users from entering the chat room directly, the system will first determine whether the newly created chat room is generated by Login, and exit if not. In the same way, such protection can also be added to the List Frame and Post Frame of the chat room. Of course, checking the user's identity and password in the List Frame and Post Frame is foolproof, but it only increases the burden on the system.
To sum up, being able to see the source code of the system is undoubtedly the beginning of danger, so try to write the system in Php without seeing the source code; setting up a chat room opened by Login hides the browser toolbar and status bar, etc., and the right mouse button and shortcut keys are blocked.

4. Summary

Chat rooms written in Php are efficient and stable, and are the best choice for writing network interactive programs.

References:
[1] by Rasmus Lerdorf. PHP Manual [M]. Electronic Documentation, 2000


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/316554.htmlTechArticlePHP Chat Room Technology Huang Guohui 1. Preface Chatting online is the most popular way to make friends nowadays. The chat rooms launched by major websites have their own characteristics. Chat rooms are mainly divided into two types: WebChat and BBSChat. ...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!