Principle:
1. The first user browses a page.
2. The server program reads the number of pages viewed from the database or file.
3. Add the number of times to a store and send it back to the first user.
4. The second user browses a page.
5. The server program reads the number of pages viewed from the database or file.
6. Add the number of times to another store and send it back to the second user.
Functions that need to be understood:
fopen () Open File
FileSize () Get File size
Fseek () Move file pointer
Fgets () Gets the contents of the file pointer.
Fputs () writes a string such as the location of the file pointer
Fclose () Close file
File_exists () Determine if the file exists
EXEC () executes external programs
The simplest registers:
<title>Guest Counter prototypes</title>
/*
(c) 1998 David W. Bettis
Here is the copyright information
*/
$counterFile = "Counter.txt";
#这里是定义记数器文件
function Displaycounter ($counterFile) {
$fp = fopen ($counterFile, "RW");
#打开文件, read and write
$num = Fgets ($fp, 5);
#取得当前数字
$num + = 1;
#加1
Print "You are the first". " $num "." A bit of boredom ";
EXEC ("Rm-rf $counterFile");
EXEC ("echo $num > $counterFile");
#偷懒的方式哦, do not write using fputs
}
if (!file_exists ($counterFile)) {
EXEC ("Echo 0 > $counterFile");
} #如果记数器文件不存在, create it and set the content to 0
Displaycounter ($counterFile);
?>
PHP Register comparison simple version:
#版权没有啦, that's so easy.
$FP =fopen ("Counter.txt", "r+");
Flock ($FP, 3);
#打开记数器文件并锁住
$fsize =filesize ("Count.txt");
$count =fgets ($fp, $fsize + 1);
$count + +;
#取得数码并加一
Fseek ($fp, 0);
Fputs ($fp, $count);
Fclose ($FP);
#将新数码写入文件
echo "You are the first $count visitor";
?>
PHP Register graphical version:
Make 10 pictures, set up the numbers with pictures, I don't elaborate
Suppose the picture is 0.gif ~ 9.gif
.... $count for the obtained value
$strcount =strval ($count);
$strcount =chop ($strcount);
$countlen = $strlen ($strcount);
$shtml = "";
for ($i =0; $i < $countlen; $i + +) {
$shtml. = "$shtml. = $strcount [$i];
$shtml. = ". gif ' >";
}
Echo $shtml;
?>
PHP Register Database version:
Using SQL registers, first build the table
CREATE TABLE counter
(
counter int NOT NULL,
ID int NOT NULL
)
INSERT into counter (counter,id) VALUE (0,1)
$c ...);
#MySQL数据库连接
$sql = "SELECT * from Counter";
$result =mysql_query ($sql, $conn);
$objresult =mysql_fetch_object ($result);
$count = $objresult->counter;
$count + +;
$sql = "Update counter set counter=". $count. " where id=1 ";
mysql_query ($sql, $conn);
Mysql_close ($conn);
echo "You are the first $count visitor";
?>
The above introduces the most simple PHP program-register, including the content of the register, I hope the PHP tutorial interested in a friend helpful.