The simplest PHP Program-calculator

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.
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 Program

The simplest calculator:
<HTML>
<Head>
<Title> visitor counter prototype </title>
</Head>
<Body>
<? PHP
/*
(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 );

?>
</Body>
</Html>
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.