Php traffic statistics function implementation code

Source: Internet
Author: User
The code of the traffic statistics function implemented by php is only for learning and communication, and there must be something wrong with it. Please forgive me for the traffic statistics function
Display effect:
Total traffic: 399
Today's traffic: 14
Yesterday's traffic: 16
This code is only for learning and communication, and there must be something wrong with it. Sorry!
--
-- Table structure 'mycounter'
--
Copy codeThe 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;

The function process is as follows:
Copy codeThe 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:
Copy codeThe code is as follows:
Session_start ();
If (! Isset ($ _ COOKIE ["user"]) {
Setcookie ("user", "newGuest", time () + 3600 );
} Else {
Setcookie ("user", "oldGuest ");
}
?>

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.