php traffic statistics function implementation code
- 2020-05-26 07:54:43
- OfStack
Flow statistics function
Display effect:
Total visits: 399
Today's flow: 14
Yesterday's flow: 16
This code is for learning and communication only, there must be something wrong with it. Please forgive me!
--
-- the structure of the table 'mycounter'
--
The function process is as follows:
Of course, you need to start at line 1 of the file with the following code:
Display effect:
Total visits: 399
Today's flow: 14
Yesterday's flow: 16
This code is for learning and communication only, there must be something wrong with it. Please forgive me!
--
-- the structure of the table 'mycounter'
--
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:
<?PHP
public function ShowMyCounter(){
// Define variables
$IsGone = FALSE;
// Read the data
$querysql = "SELECT * FROM `mycounter` WHERE id = Ƈ' ";
$queryset = mysql_query($querysql);
$row = mysql_fetch_array($queryset);
// Acquisition time quantity
$DateNow = date('Y-m-d');
$RecordDate = $row['RecordDate'];
$DateNow_explode = explode("-",$DateNow);
$RecordDate_explode = explode("-",$RecordDate);
// Judge whether it is past 1 day
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;
// According to the IsGone Operate accordingly
IF($IsGone) {
$RecordDate = $DateNow;
$CounterToday = 0;
$CounterLastDay = $row['CounterToday'];
$upd_sql = "update mycounter set RecordDate = '$RecordDate',CounterToday = '$CounterToday',CounterLastDay = '$CounterLastDay' WHERE id = Ƈ' ";
mysql_query($upd_sql);
}
// Get the data again
$querysql = "SELECT * FROM `mycounter` WHERE id = Ƈ' ";
$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 = Ƈ' ";
$myquery = mysql_query($upd_sql);
}
echo " Total visits: ".$Counter;
echo "
";
echo " Today's traffic: ".$CounterToday;
echo "
";
echo " Yesterday's flow: ".$CounterLastDay;
}else{// If the database is empty, the corresponding operation
}
}
?>
Of course, you need to start at line 1 of the file with the following code:
<?PHP
session_start();
if( !isset($_COOKIE["user"]) ){
setcookie("user","newGuest",time()+3600);
}else {
setcookie("user","oldGuest");
}
?>