Home  >  Article  >  Backend Development  >  Source program download A simple PHP entry source program

Source program download A simple PHP entry source program

WBOY
WBOYOriginal
2016-07-29 08:33:281469browse

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



<br><body> <br>This chat room requires the use of page frames, your browser cannot be used <br></body> <br>


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);
}
?>

method=post>






匿称: 发言:




程式先检查是否有输入字串,若无匿名及发言内容字串则显示发言的表单 (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);
?>


上面的程式就是使用 Client Pull 的技术,每五秒就重新更新一次。同样地,它也 require 共用的 env.inc 档,要改变其中的变数时,马上就可以让所有的程式用到,这对开发网站来说,是蛮重要的方法,可以将网页程式中都会出现的地方。例如 Copyright (C) 1996-2000 的字串,放在一个档案上,到了新的一年,只要改一个档,整个站都改了。
if (!file_exists($chatfile)) {
  echo "尚未开张";
  exit;
}
$uniqfile=$tempdir.uniqid(rand());
$shellcmd="/usr/bin/tail -50 ".$chatfile. " > ".$uniqfile;
passthru($shellcmd);
程式先检查有没有使用者发送聊天内容的档案 /tmp/abc,若没有就显示尚未开张,等使用者送聊天内容。若已有聊天资料,就抓出最后五十笔,在在另外的档案中准备显示。
$chatfilearray=file($uniqfile);
$j=count($chatfilearray);
for ($i=1; $i<=$j; $i++) {
  echo $chatfilearray[$j-$i]."
n";
}
unlink($uniqfile);
将档案读入阵列变数 $chatfilearray 中,并以最后的资料最先显示的方式送给浏览器端,当然可以使用对阵列排序的方法,但确定一定时最后存入的资料在最后面,将它排序实在很浪费 CPU 时间,因此就从最后 echo 到最前面的资料。使用完成还要用 unlink() 指令,将临时档杀掉。

以上就介绍了源程序下载 一个简单的PHP入门源程序,包括了源程序下载方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

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