Quoting the example of "Starry Sky Prodigal" PHP Chinese document:
Chat room is the secret weapon for boring people on the website. At the same time, the webmaster or other personnel can also kill time here. Even if you have a vigorous online romance, you can at least increase your typing speed.
A chat room is actually a CGI program used by multiple people. The program sorts the strings entered by each person according to the time when the system receives them, and then sends them to each user. The difference between Web chat rooms and BBS chat rooms is that BBS chat rooms can immediately distribute every sentence received to every Internet user in the chat room; Web CGI programs cannot always connect like BBS telnet. line, Web CGI must send information out as quickly as possible and then end the connection. This situation occurs because Web chat rooms still use the HTTP transmission protocol. The version of HTTP implementation, whether it is version 0.9, 1.0 or 1.1, cannot occupy the network connection port for a long time.
In order to solve the problem of data being unable to be transferred immediately and the problem of updating information, Netscape used new technologies after version 3.0 of the browser, and Internet Explorer also implemented these technologies developed by Netscape. Netscape divides it into two technologies: Server Push and Client Pull. Server Push uses multiple MIME encodings from the Web server to send data to the user. Currently, few websites use this method; while Client Pull uses the HTML meta tag and the http-equiv="Refresh" attribute. , indicating that the data needs to be reloaded. As for the loading time, the content attribute is used to achieve it.
tags are usually placed in the section of
.. so that the browser can prepare to update the user-side web page. The following is an example of meta and PHP, set to reload every fifteen seconds.
">
If you don’t use Server Push or Client Pull to make a chat room, is there any other way to make the Web Can the browser chat? The answer is yes. You can use Java or ActiveX (limited to IE4 and 5) or even develop your own Browser Plug-in program (such as Qimo's chat room), but this has nothing to do with PHP.
In addition, because all netizens’ messages are updated regularly, in order to avoid half-written strings being cleared due to refresh, the chat room is framed using frame technology. It is necessary to do it. The following example is the main program of the chat room.
Chat room In the program, frame is used When creating two PHP programs, it is recommended to put them in the same directory, such as /chatroom, for future maintenance. In addition, in order to use the same variables for list.php and post.php, the following example places the common path. env.inc, you can put it in /chatroom or the PHP include setting path of the web server (such as Apache)
// File name: env.inc
$tempdir="/tmp. /";
$chatfile="/tmp/abc";
?>
The backend of the chat room can be designed to be very simple. You can simply use files to do it. You can also create a database and throw the chat content into it. , if you really care about system efficiency, you may consider using UNIX's process communication IPC.
This section is about putting the content of user comments into files. Most of the examples here use UNIX/Linux external commands. If the system does not have this command (or program), please install the relevant program yourself.
In fact, throwing data into a file is faster than using a database. If you still care about speed, you can install a RAM Disk in the UNIX machine, and then set the access path of the file to the RAM Disk to ensure storage. The speed can meet stringent requirements. Some websites that market themselves as high-speed search engines even put the entire database information into a RAM Disk, which immediately increases the system speed by ten or a hundred times. Moreover, the price of RAM is relatively cheap compared with other solutions. If you use Windows NT, you have no choice but to wait and see when Microsoft provides it, or use Third Party products.
Some users may not be very familiar with UNIX. Here is a brief introduction to the commands used:
touch: Create a new file, or modify the last update date of an old file.
echo plus two greater than symbols: redirect the string display to the specified place.
tail: Display the last few rows of data in the file. The default value is ten rows. You can use the minus sign plus a number to modify the number of rows to be displayed.
The following is a program for sending and processing message strings. The program uses the env.inc file.
// 档名: post.php
require("env.inc");
if (($chatuser!="") and ($chattext!="")) {
$chatstr="
".date("h:i:s")."-
".$chatuser.": ".$chattext;
$cmdstr="echo "".$chatstr."" >> ".$chatfile;
if (!file_exists($chatfile)) passthru("touch ".$chatfile);
passthru($cmdstr);
}
?>
程式先检查是否有输入字串,若无匿名及发言内容字串则显示发言的表单 (Form),若有资料则将字串及当时时间存入档案中 (利用 UNIX 的转向指令)。当然,为了防止错误,先检查是否有档案可存档,若没有则先 touch 该档,例中的档案就是 /tmp/abc。
">
// 档名: list.php
require("env.inc");
if (!file_exists($chatfile)) {
echo "尚未开张";
exit;
}
$uniqfile=$tempdir.uniqid(rand());
$shellcmd="/usr/bin/tail -50 ".$chatfile. " > ".$uniqfile;
passthru($shellcmd);
$chatfilearray=file($uniqfile);
$j=count($chatfilearray);
for ($i=1; $i<=$j; $i++) {
echo $chatfilearray[$j-$i]."
n";
}
unlink($uniqfile);
?>