The simplest PHP program--Register

Source: Internet
Author: User
Tags count file size
Principle:
1. The first user browses to a page.
2. The server program reads the number of times the page has been browsed from the database or file.
3. Add the number of times to the store and return it to the first user.
4. Second user browses to a page.
5. The server program reads the number of times the page has been browsed from the database or file.
6. Add one more storage and send it back to the second user.
Functions to understand:
fopen () Open File
FileSize () Get File size
Fseek () Move file pointer
Fgets () Gets the contents of the file pointer.
Fputs () writes the string as the location of the file pointer
Fclose () Close file
File_exists () to determine whether a file exists
EXEC () Execute external program

The simplest registers:
<title> Visitor Counter Prototypes </title>
<body>
<?php
/*
(c) 1998 David W. Bettis
Here is the copyright information
*/

$counterFile = "Counter.txt";
#这里是定义记数器文件

function Displaycounter ($counterFile) {
$fp = fopen ($counterFile, "RW");
#打开文件, using read and Write methods

$num = Fgets ($fp, 5);
#取得当前数字

$num + 1;
#加1

Print "You are the first." $num "." "a boring person";
EXEC ("Rm-rf $counterFile");
EXEC ("echo $num > $counterFile");
#偷懒的方式哦, do not use fputs write
}

if (!file_exists ($counterFile)) {
EXEC ("Echo 0 > $counterFile");
#如果记数器文件不存在, create it and set the content to 0

Displaycounter ($counterFile);

?>
</body>
PHP Registers Compare simple version:
?
#版权没有啦, it's so simple.

$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 graphic version:
Make 10 pictures, put the numbers in the picture group, I won't elaborate.
Suppose the picture is 0.gif ~ 9.gif

?
... $count for the value obtained
$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, you build your table first.
CREATE TABLE counter
(
counter int NOT NULL,
Idint NOT NULL
)
INSERT into counter (counter,id) VALUE (0,1)

?
$conn =mysql_connect (..., ...);
#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";
?> turn from: Dynamic Network production guide www.knowsky.com

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.