File and database-based counter _ PHP Tutorial

Source: Internet
Author: User
Counters based on files and databases. Author: javaduke, originally created on www.dukejava.com file-based php counter <〈? Php counters, based on the file system. Functionwincounter () {if the file does not exist, create it by javaduke, originally at www.dukejava.com
File-based php counters
<〈?
// Php counters, based on the file system.
Function wincounter (){
// If the object does not exist, create it
If (! File_exists ("count.txt "))
{
Exec ("echo 0> count.txt ");
}
// Open our record file
// Obtain the file size and obtain the required data based on the file size.
$ Fp = fopen ("count.txt", "r + );
$ FileSize = filesize ("count.txt ");
$ Count = fgets ($ fp, $ FileSize + 1 );
// Add the number of records to 1 and save the records to the file.
$ Count + = 1;
Fseek ($ fp, $ Count );
Fclose ($ fp );
// Returns the number of shards.
Return $ Count;
}
? > 〉
Database-based counters (mysql )?
1. create a database first:
Create table counter {
Counter int not null,
Id int not null
}
Insert into counter (counter, id) VALUES (0, 1)
2. COUNTER Code:
<〈?
// PHP counter, based on the MySQL database server.
Function linuxcounter (){
// Connect to the MySQL database
$ Conn = mysql_connect ("localhost", phpbook ","");
// Query the current number of views
// Note the method for obtaining the result
$ SQL = "select * from counter ";
$ Result = mysql_query ($ SQL, $ conn );

$ ObjResult = mysql_fetch_object ($ result );
$ Count = $ objResult-> counter;
// Update the database and return the current browser count as the result
$ SQL = "update counter set counter =". ($ cont + 1). "where id = 1 ";
Mysql_query ($ SQL, $ conn );
Mysql_close ($ conn );
Return $ count + 1;
}
? > 〉

Compile file-based php counters <〈? // Php counters, based on the file system. Function wincounter () {// if the file does not exist, create it...

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.