Home  >  Article  >  Backend Development  >  A simple PHP entry source program_PHP tutorial

A simple PHP entry source program_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 16:04:25712browse

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 a vigorous online romance occurs, even if it doesn't happen, it can at least increase the 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 not being transferred immediately and the problem of updating information, Netscape used new technologies after the 3.0 version 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 create a chat room, is there any other way to enable chatting in a Web browser? The answer is yes. You can use Java or ActiveX (limited to IE4 and 5) or even develop your own dedicated Browser Plug-in program (such as Qimo's chat room), but this has nothing to do with PHP and is not our focus.

In addition, since all netizens’ messages are updated regularly, in order to avoid half-written strings being cleared due to refresh, the chat room is built using frame technology. It is necessary. The following example is the main program of the chat room.




Chat Room




<br><body> <br>This chat room requires the use of page frames, which your browser cannot use <br></body> <br>
< ;/frameset>





The program uses frame to bring out two PHP programs. It is recommended to put them in the same directory, for example/ chatroom for future maintenance. In addition, in order for list.php and post.php to use the same variables, the following example places the common variable path in env.inc. You can place it in /chatroom or the PHP include of the web server (such as Apache) in the set path.


// File name: env.inc
$tempdir="/tmp/";
$chatfile="/tmp/abc";
?>


The back-end 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 it is really If you care about system efficiency, you may consider using UNIX's IPC for process communication.

This section will put the content of the user's message into a file. 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 set the file access path to the RAM. On the Disk, the access speed is guaranteed to 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 an 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 lines of data in the file. The default value is ten lines. You can use the minus sign plus a number to modify the number of lines to be displayed.

The following is a program for sending and processing message strings. The program uses the env.inc file.


// File name: post.php
require("env.inc");
if (($chatuser!="") and ($chattext!="")) {
$chatstr="".date("h:i:s")."-: ".$chattext;
$cmdstr="echo "".$chatstr."" >> ".$chatfile;
if (!file_exists ($chatfile)) passthru("touch ".$chatfile);
passthru($cmdstr);
}
?>

method=post>




Anonymous: Speak: < ;/td>






The program first checks whether there is an input string. If there is no anonymous and speech content string, Display the speech form (Form), and if there is data, store the string and current time in the file (using the UNIX redirect command). Of course, in order to prevent errors, first check whether there is a file that can be archived. If not, touch the file first. The file in this example is /tmp/abc.




< ;meta content="text/html; charset=gb2312" http-equiv=Content-Type>

// File name: list.php
require("env.inc");

if (!file_exists($chatfile)) {
echo "Not yet opened ";
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 );
?>




The above program uses Client Pull technology and is updated every five seconds. . Similarly, it also requires the shared env.inc file. When you want to change the variables in it, it can be used by all programs immediately. This is a very important method for developing websites. It can appear in all web programs. place. For example, the string "Copyright (C) 1996-2000" is placed in a file. In the new year, as long as one file is changed, the entire site will be changed.

if (!file_exists($chatfile)) {
echo "Not yet opened";
exit;
}

$uniqfile=$tempdir.uniqid(rand());
$shellcmd="/usr/bin/tail -50 ".$chatfile. " > ".$uniqfile;
passthru($shellcmd);

The program first checks if there is a file /tmp/abc where the user has sent chat content. If not, it will show that it has not yet been opened and waits for the user to send chat content. If there is already chat data, grab the last fifty and prepare to display it in another file.

$chatfilearray=file($uniqfile);
$j=count($chatfilearray);
for ($i=1; $i<=$j; $i++) {
echo $chatfilearray[$j-$i]."
n";
}
unlink($uniqfile);

Read the file into the array variable $chatfilearray, and Send the data to the browser with the last data displayed first. Of course, you can use the method of sorting the array, but make sure that the last data stored at a certain time is at the end. Sorting it is a waste of CPU time, so start from the end. echo to the front data. After use, you also need to use the unlink() command to kill the temporary file.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/315988.htmlTechArticleQuoting the example of the "Starry Sky Prodigal" PHP Chinese document: Chat rooms are the secret weapon for boring people on Web sites. . At the same time, the webmaster or other personnel can also kill time here. Even posted...
Statement:
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