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