Based on file, database counter

zhaozj2021-02-08  226

Author: javaduke, originated in www.dukejava.com

File-based PHP counter

// PHP counter, based on the file system.

Function WinCOUNTER () {

// If the file does not exist, create it

IF (! File_exists ("count.txt")))

{

EXEC ("echo 0> count.txt");

}

/ / Open our record file

// Get the file size then takes out the required data according to the size of this file.

$ fp = fopen ("count.txt", "r );

$ Filesize = filesize ("count.txt");

$ Count = fgets ($ FP, $ FILESIZE 1);

// Plus the number of records to 1 in the file back

$ Count = 1;

FSeek ($ FP, $ COUNT);

Fclose ($ fp);

/ / Return to the current number of visit

RETURN $ COUNT;

}

? >

Based on the database's counter (mysql)?

1. First create a database:

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 MySQL Database

$ conn = mysql_connect ("LocalHost", PHPBOOK "," ")

// Query the current browse number

/ / Pay attention to the way

$ SQL = "SELECT * from counter";

$ Result = mysql_query ($ SQL, $ conn);

$ objresult = mysql_fetch_Object ($ results);

$ count = $ objresult-> counter;

/ / Update the database and return to the current browse number as a result

$ SQL = "Update Counter Set Counter =". ($ Cont 1). "Where id = 1";

MySQL_Query ($ SQL, $ conn);

MySQL_Close ($ conn);

Return $ COUNT 1;

}

? >

转载请注明原文地址:https://www.9cbs.com/read-1733.html

New Post(0)