A simple PHP starter source program such as message Board (Chinese)--Beginners

Source: Internet
Author: User
Tags date exit count variables php and variable touch access
Program | Chinese quote "star Prodigal" PHP Chinese Document Example:

Chat room, is the WEB site to send boring people's secret weapon. At the same time, the webmaster or other personnel can also be killed here. Even if there is a vigorous network of love, even if not, at least can increase the speed of typing.
Chat rooms, in fact, are many people use the CGI program. The program will input the string of each person, according to the system received the completion of the time after finishing, and then sent to each user. and the Web chat room and bbs chat room is different place is BBS chat room can receive a sentence every word, immediately distributed to every Internet user in the chat room; web CGI must deliver the information as quickly as possible and end with a CGI program that cannot be wired like a BBS telnet. Connection. This happens because the Web chat room still uses the HTTP transport protocol, and the version of HTTP implementation, whether 0.9, 1.0, or 1.1, cannot occupy the Port of the network connection for long.

To address the problem of the inability to transmit data immediately, and to update the message, Netscape used the new technology after version 3.0 browsers, and Internet Explorer did the technology that Netscape developed. Netscape divided it into Server Push and Client Pull two technologies. The server Push is sent by the Web server to the user side with multiple MIME encodings, and the client Pull uses the META tag of HTML and uses the attributes of http-equiv= "Refresh" to indicate that the resource Material to reload, as for loading time, use the content attribute to achieve.

<meta> tags are usually placed in
<meta http-equiv= "Refresh" content= "15; url=<? echo $PHP _self?> ">

If you do not use Server Push or Client Pull to do chat rooms, is there any other way to allow Web browsers to chat? The answer is yes. You can use either Java or ActiveX (limit IE4, 5) to do even self-developed proprietary Browser Plug-in programs (such as the's chat room), but this is not related to PHP, not our focus.

In addition, due to the regular update of all the users of the message, in order to be afraid of writing half because of the refresh has been cleared off the writing string, so the chat room to frame the page frame technology to do is necessary. The following example is the main program for the chat room.


<title> Chat Room </title>
<frameset rows= "*,40" border=1>
<frame src= "list.php" Name=list scrolling=auto>
<frame src= "post.php" Name=post scrolling=no>
<noframes>
<body>
This chat room needs to use the page box, your browser can not use
</body>
</noframes>
</frameset>




The program takes a frame with two PHP programs and recommends that they be placed in the same directory, such as/chatroom, for later maintenance. In addition, for list.php and post.php to use the same variables, the following example places a common variable path in Env.inc, which can be placed in/chatroom or the PHP include setting path of a WEB server (such as Apache).


<?php
File name: Env.inc
$tempdir = "/tmp/";
$chatfile = "/TMP/ABC";
?>


The back end of the chat room can be designed very simple, simple use of files to do, you can also get a database, the content of the chat into, if really care about the efficiency of the system, you may consider using UNIX travel communication IPC.

This section puts the content of the user's message into the file, and most examples here use Unix/linux external directives. If the system does not have this instruction (or program), please install the relevant program.

In fact, the data into the file will be faster than using the database, if you still care about speed, you can load the UNIX machine RAM disk, and then the file access path are located on the RAM disk, to ensure access speed to meet stringent requirements. In some Web sites that call for a high speed search engine, even putting the entire database into RAM Disk, the system speeds up to 10 times times faster, and the price of RAM is cheap compared to other solutions. If you use Windows NT, then there is no way to see when Microsoft offers, or use the third party's products.

Some users may not be familiar with UNIX, and here is a brief introduction to the instructions that will be used:

Touch: Creates a new file, or modifies the last updated date of the old file.

Echo plus two greater-than symbol: Turns the string display to the specified place.

Tail: Display the last few lines of data, the default value is 10 lines, you can use the minus sign plus number, modify the number of rows to display.

The following procedures for sending and processing message strings are used to env.inc files.


<?php
File name: post.php
Require ("Env.inc");
if (($chatuser!= "") and ($chattext!= "")) {
$chatstr = "<font color=8080ff>". Date ("H:i:s"). " </font>-<font color=ff8080> ". $chatuser." </font&gt: ". $chattext;
$CMDSTR = "echo \" ". $chatstr." ">>". $chatfile;
if (!file_exists ($chatfile)) PassThru ("Touch". $chatfile);
PassThru ($CMDSTR);
}
?><body bgcolor=ffffff leftmargin=0 topmargin=0 marginheight=0 marginwidth=0>
<form action=<? echo $PHP _self;?> method=post>
<table border=0 width=100%><tr>
&LT;TD align=right> Nickname:</td>
<td><input type=text name=chatuser size=8 value= " echo $chatuser?> "></td>
<td Align=right> spoke:</td>
<td><input type=text name=chattext size=30 maxlength=500></td>
<td><div align=right><input type=submit value= "Send out" ></td>
</tr></table>
</form>
</body>


The program checks to see if there are any input strings, and if there are no anonymous and spoken words, the form (form) is displayed, and if there is information, the string and time is stored in the file (using UNIX's steering instructions). Of course, in order to prevent errors, first check whether there are files to be archived, if not first touch the file, example of the file is/TMP/ABC.


<meta http-equiv= "Refresh" content= "5; url=<? echo $PHP _self?> ">
<meta content= "text/html; charset=gb2312 "http-equiv=content-type>
<body bgcolor=ffffff leftmargin=0 topmargin=0 marginheight=0 marginwidth=0>
?
File name: list.php
Require ("Env.inc");

if (!file_exists ($chatfile)) {
echo "has not yet opened </body>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]. " <br>\n ";
}
Unlink ($uniqfile);
?>
</body>


The above program is to use the Client Pull technology, every five seconds to update again. Similarly, it is also require common Env.inc file, to change the variables, you can immediately get all the programs to use, this is a very important way to develop the Web site, can be a Web page program will appear in the place. For example, Copyright (C) 1996-2000 of the string, placed in 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 </body>Exit
}

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

The program first checks if the user sent the chat content of the file/tmp/abc, if not on the display has not yet opened, and other users to send chat content. If you already have chat information, grab the last 50 strokes and prepare to display them in another file.

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

Read the file into the array variable $chatfilearray, and the last data to display the first way to the browser end, of course, you can use the method of sorting the column, but to determine the last deposit in the last level of information, it is very wasteful to sort it out of CPU time, so from the last Echo To the front of the information. Use the completion of the unlink () command, the temporary file to kill.



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.