Php statistics website/html page browsing Visits Program-PHP source code

Source: Internet
Author: User
This article introduces some commonly used website statistics code written by php, and provides statistics code for viewing resources on databases and html static pages, for more information, see. This article introduces some commonly used website statistics code written by php, and provides statistics code for viewing resources on databases and html static pages, for more information, see.

Script ec (2); script

Instance 1

Code for directly using txt files for Statistics

The Code is as follows:

Session_start (); // defines the session. logon from the same IP address is not accumulated.
$ Filepath = 'count.txt ';
If ($ _ SESSION ['temp '] = '') // judge whether the value of $ _ SESSION [temp] is null, where temp is a custom variable.
{
If (! File_exists ($ filepath) // check whether the file exists. If no file exists, the file is created and assigned 0.
{
$ Fp = fopen ($ filepath, 'w ');
Fwrite ($ fp, 0 );
Fclose ($ fp );
Counter ($ filepath );
} Else
{
Counter ($ filepath );
}
$ _ SESSION ['temp '] = 1; // After logon, assign a value of 1 to $ _ SESSION [temp ].
}
Echo 'Welcome to the lazy webmaster's clip website. You are the site '. file_get_contents ($ filepath). 'guest ';
// The counter () method is used to obtain the number in the file.

Function counter ($ f_value)
{
// When you open a file in w mode, the content in the file is cleared. Therefore, open the file in r mode first, retrieve the file content, and save it to the variable.
$ Fp = fopen ($ f_value, 'R') or die ('An error occurred while opening the file. ');
$ CountNum = fgets ($ fp, 1024 );
Fclose ($ fp );
$ CountNum ++;
$ Fpw = fopen ($ f_value, 'w ');
Fwrite ($ fpw, $ countNum );
Fclose ($ fpw );
}
// Comment out the following line to achieve the same IP login without accumulating effect, can be opened during testing
Session_destroy ();
?>

The above uses a txt file. Next we will introduce a mysql database operation instance.

The Code is as follows:

Create table 'mycounter '(
'Id' int (11) not null auto_increment,
'Counter' int (11) not null,
'Counterlastday' int (10) default NULL,
'Countertoday' int (10) default NULL,
'Recorddate' date not null,
Primary key ('id ')
) ENGINE = InnoDB default charset = gbk AUTO_INCREMENT = 2;

Function

The Code is as follows:
Public function ShowMyCounter (){
// Define variables
$ IsGone = FALSE;
// Read data
$ Querysql = "SELECT * FROM 'mycounter 'WHERE id = comment '";
$ Queryset = mysql_query ($ querysql );
$ Row = mysql_fetch_array ($ queryset );
// Obtain the time amount
$ DateNow = date ('Y-m-d ');
$ RecordDate = $ row ['recorddate'];
$ DateNow_explode = explode ("-", $ DateNow );
$ RecordDate_explode = explode ("-", $ RecordDate );
// Determine whether the previous day has passed
If ($ DateNow_explode [0]> $ RecordDate_explode [0]) $ IsGone = TRUE;
Else if ($ DateNow_explode [0] ==$ RecordDate_explode [0]) {
If ($ DateNow_explode [1]> $ RecordDate_explode [1]) $ IsGone = TRUE;
Else if ($ DateNow_explode [1] ==$ RecordDate_explode [1]) {
If ($ DateNow_explode [2]> $ RecordDate_explode [2]) $ IsGone = TRUE;
} Else BREAK;
} Else BREAK;
// Perform operations based on IsGone
IF ($ IsGone ){
$ RecordDate = $ DateNow;
$ CounterToday = 0;
$ CounterLastDay = $ row ['countertoday'];
$ Upd_ SQL = "update mycounter set RecordDate = '$ recorddate', CounterToday =' $ countertoday', CounterLastDay = '$ CounterLastDay' WHERE id = comment '";
Mysql_query ($ upd_ SQL );
}
// Obtain data again
$ Querysql = "SELECT * FROM 'mycounter 'WHERE id = comment '";
$ Queryset = mysql_query ($ querysql );
$ Counter = $ row ['counter'];
$ CounterToday = $ row ['countertoday'];
$ CounterLastDay = $ row ['counterlastday'];
If ($ row = mysql_fetch_array ($ queryset )){
If ($ _ COOKIE ["user"]! = "OldGuest "){
$ Counter = ++ $ row ['counter'];
$ CounterToday = + $ row ['countertoday'];
$ Upd_ SQL = "update mycounter set counter = '$ counter', CounterToday =' $ CounterToday 'WHERE id = comment '";
$ Myquery = mysql_query ($ upd_ SQL );
}
Echo "total access volume:". $ Counter;
Echo"
";
Echo "Today's traffic:". $ CounterToday;
Echo"
";
Echo "Yesterday's traffic:". $ CounterLastDay;
} Else {// the corresponding operation if the database is empty
}
}
?>

Of course, the following code must be written at the first line of the file:

The Code is as follows:

Session_start ();
If (! Isset ($ _ COOKIE ["user"]) {
Setcookie ("user", "newGuest", time () + 3600 );
} Else {
Setcookie ("user", "oldGuest ");
}
?>

If it is a static page, the above method cannot be implemented, but the following is a good statistical example.

The Code is as follows:




Insert title here




Php code for counting the number of visits to a static html page


Count. php code

The Code is as follows:

$ Aid = isset ($ _ GET ['aid '])? $ _ GET ['aid ']: '';
$ T = isset ($ _ GET ['T'])? $ _ GET ['T']: '';
If (intval ($ aid )){
If ($ t = 'show '){
Echo "document. write ('here is the number of views that can be read from the database ');";
}
Else {
$ Conn = mysql_connect ('localhost', 'root', 'root ');
$ SQL = "Update count set click_num = click_num + 1 where aid = '$ aid '";
Mysql_db_query ('db _ test', $ SQL, $ conn );
}
}
?>

Database

The Code is as follows:

--
-- Table structure 'Count'
--
Create table if not exists 'Count '(
'Id' int (11) not null auto_increment,
'Id' int (11) default NULL,
'Click _ num' int (11) default NULL,
Primary key ('id ')
) ENGINE = InnoDB default charset = gbk AUTO_INCREMENT = 2;

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.