The simplest PHP program-the stenographer

Source: Internet
Author: User
Principle: 1. the first user browses a page. 2. the number of times the server program reads the page from the database or file. 3. store the number of times and send it back to the first user. 4. the second user browsed a page. 5. the number of times the server program reads the page from the database or file. 6. store the number of times and send it back to the second user. & Nbsp; function: fopen () to open the file filesize () principle:
1. the first user browses a page.
2. the number of times the server program reads the page from the database or file.
3. store the number of times and send it back to the first user.
4. the second user browsed a page.
5. the number of times the server program reads the page from the database or file.
6. store the number of times and send it back to the second user.
Functions to be understood:
Fopen () open a file
Filesize () to get the file size
Fseek () move file pointer
Fgets () obtains the row content of the file pointer.
Fputs () writes the string as the position of the file pointer.
Fclose () close the file
File_exists () to determine whether a file exists
Exec () executes external programs
 
The simplest calculator:
 
 
Visitor counter prototype 
 
 
/*
(C) 1998 David W. Bettis
Here is the copyright information
*/

$ CounterFile = "counter.txt ";
# Here is the definition counter file

Function displayCounter ($ counterFile ){
$ Fp = fopen ($ counterFile, "rw ");
# Open a file and use the read/write method

$ Num = fgets ($ fp, 5 );
# Obtain the current number

$ Num + = 1;
# Add 1

PRint "you are the". "$ num". "bit boring ";
Exec ("rm-rf $ counterFile ");
Exec ("echo $ num> $ counterFile ");
# The Method of laziness. do not use fputs for writing.
}

If (! File_exists ($ counterFile )){
Exec ("echo 0> $ counterFile ");
} # If the recorder file does not exist, create it and set the content to 0.

DisplayCounter ($ counterFile );

?>
 
 
PHP stenographer is relatively simple:
# No copyright, so easy

$ Fp = fopen ("counter.txt", "r + ");
Flock ($ fp, 3 );
# Open the recorder file and lock it

$ Fsize = filesize ("count.txt ");
$ Count = fgets ($ fp, $ fsize + 1 );
$ Count ++;
# Obtain the digital and add one

Fseek ($ fp, 0 );
Fputs ($ fp, $ count );
Fclose ($ fp );
# Writing new digital data into a file

Echo "you are the $ count visitor ";
?>
 
PHP stenographer graphic version:
I will not elaborate on creating 10 images and concatenating numbers with pictures.
Fake Image: 0.gif ~ 9. gif

... $ Count is 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 stenographer database version:
Use the SQL calculator to create a 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 database connection

$ 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 $ count guest ";
?>

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.