SOURCE program Download a simple PHP starter source program

Source: Internet
Author: User
Cite the example of the "Star Prodigal" php Chinese Document:
Chat room is the secret weapon of the Web station to send boring people. At the same time, the webmaster or other personnel can also be killed in this place. Even a vigorous network love, even if not, at least can increase the speed of typing.
The chat room is actually a CGI program used by many people. The program will enter each person's string, according to the system received the completion of the time after the collation, and then to the various users. And the web chat room and BBS chat room is different from BBS chat room can receive every word, immediately distributed to each in the chat room network users; Web CGI cannot be wired as a BBS telnet, and a web-based CGI must send the information at the fastest speed and then end Connection. This happens because the Web chat room is still using the HTTP transport protocol, and in the HTTP version, either 0.9, 1.0, or 1.1 will not be able to occupy the Port of the network for a long time.
To solve the problem of data not being transmitted immediately, and to update the message, Netscape used new technology after the 3.0 version of the browser, and Internet Explorer implemented the technology developed by these Netscape. Netscape it into Server Push and Client pull two technologies. Server Push is used by Web servers to encode data in multiple MIME encodings to the consumer side, which is currently less available, while Client pull leverages the HTML meta tag and uses the http-equiv= "Refresh" attribute to indicate Loading time is achieved by using the content attribute.
Labels are usually placed in..section so that the browser can only prepare to update the consumer's Web page earlier. Here is an example of meta and PHP sharing, set to reload every 15 seconds.
">
If you do not use Server Push or Client pull to do chat rooms, is there any other way to allow the Web browser to chat? The answer is yes. You can use Java or ActiveX (limited to IE4, 5) to do even self-development of proprietary Browser plug-in programs (such as Chimo chat rooms), but this is not related to PHP, not our focus.
In addition, due to the regular update of all users of the message, in order to be afraid to write half because of the refresh and was cleared off the string has not yet been written, so the chat room frame of the page frame technology to do is necessary. The following example is the main program of the chat room.


<title>Chat room</title>




&lt;noframes&gt;&amp;lt;br&amp;gt;&amp;lt;body&amp;gt; &amp;lt;br&amp;gt; This chat room requires a page box that your browser cannot use &amp;lt;br&amp;gt;&amp;lt;/body&amp;gt; &amp;lt;br&amp;gt;&lt;/noframes&gt;


Two PHP programs with frame in the program, it is recommended to put them in the same directory, such as/chatroom, for future maintenance. In addition, in order for list.php and post.php to use the same variables, the following example places common path paths in Env.inc, which can be placed in the PHP include settings path of the/chatroom or Web server (such as Apache).
File name: Env.inc
$tempdir = "/tmp/";
$chatfile = "/TMP/ABC";
?>
Chat Room backend can be designed very simple, simple use of files to do, you can also get a database, the content of the chat dropped into, if really care about the system efficiency, may consider the use of UNIX travel communication IPC.
In this section, the contents of the user's message are put into the file, and most of the examples here use Unix/linux's external instructions. If the system does not have this instruction (or program), please install the relevant program yourself.
In fact, the data into the file will be faster than the use of the database, if you still care about the speed, you can load the UNIX machine RAM disk, and then the file access path is set on the RAM disk, to ensure that access speed can meet the stringent requirements. At some sites called high-speed search engines, even the entire database is put into RAM Disk, which immediately increases the system speed by 10 times times, and the price of RAM is very cheap compared to other solutions. If you use Windows NT, there is no way to see when Microsoft is offering, or using third party products.
Some users may not be familiar with UNIX yet, here is a brief introduction to the instructions that will be used:
Touch: Create a new file, or modify the last update date for the old file.
Echo plus two greater than symbol: turns the string display to the specified place.
Tail: Displays the data of the last row of the file, the default value is 10 lines, you can use the minus sign plus the number, modify the number of lines to display.
Below is the program that sends out and processes the message string, the program uses the Env.inc file.
File name: 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);
}
?>




The program checks to see if there is an input string, and if no anonymous and spoken content string displays the statement form (form), the string and the time of the data are stored in the file (using the UNIX steering instructions). Of course, in order to prevent errors, first check if there is a file can be archived, if not the first touch the file, example of the file is/TMP/ABC.

">


File name: list.php
Require ("Env.inc");
if (!file_exists ($chatfile)) {
echo "has 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 is a technique that uses Client pull, which is updated every five seconds. Similarly, it also require common Env.inc file, in order to change the variables, you can immediately let all the program to use, which for the development of the site, is a very important way to the Web program will appear in the place. For example, Copyright (C) 1996-2000 of the string, placed on a file, to the new year, as long as the change of a file, the whole station has been changed.
if (!file_exists ($chatfile)) {
echo "has not yet opened";
Exit
}
$uniqfile = $tempdir. Uniqid (rand ());
$shellcmd = "/usr/bin/tail-50". $chatfile. ">". $uniqfile;
PassThru ($shellcmd);
The program first check that there is no user to send chat content files/tmp/abc, if not the display has not yet opened, and so on users send chat content. If you already have a chat, grab the last 50 strokes and prepare the display 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 the last data is displayed first to the browser side, of course, you can use the method to sort the column, but to determine the last data deposited at the end, it is very wasteful CPU time, so from the last Echo To the front of the data. Use the unlink () command to kill the temporary file.

The above describes the source program to download a simple PHP source program, including the source program download content, I hope to be interested in PHP tutorial friends helpful.

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