首頁 >後端開發 >PHP問題 >php 怎麼實現前端統計流量

php 怎麼實現前端統計流量

藏色散人
藏色散人原創
2021-12-17 09:45:372284瀏覽

php實現前端統計流量的方法:1、建立一個PHP範例檔案;2、定義變數;3、讀取資料;4、判斷是否已過去一天;5、根據IsGone進行對應操作即可。

php 怎麼實現前端統計流量

本文操作環境:Windows7系統,PHP7.1版,Dell G3電腦。

php 怎麼實作前端統計流量?

php流量統計功能的實作代碼

用php實作的流量統計功能碼,本程式碼僅供學習交流,其中必有不妥之處。

流量統計功能 

顯示效果: 

总访问量:399 
今日流量:14 
昨日流量:16
-- 
-- 表的结构 `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 ;

函數過程如下: 

<?PHP 
public function ShowMyCounter(){ 
//定义变量 
$IsGone = FALSE; 
//读取数据 
$querysql = "SELECT * FROM `mycounter` WHERE id = Ƈ&#39; "; 
$queryset = mysql_query($querysql); 
$row = mysql_fetch_array($queryset); 
//获得时间量 
$DateNow = date(&#39;Y-m-d&#39;); 
$RecordDate = $row[&#39;RecordDate&#39;]; 
$DateNow_explode = explode("-",$DateNow); 
$RecordDate_explode = explode("-",$RecordDate); 
//判断是否已过去一天 
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; // www.jbxue.com
}else BREAK; 
//根据IsGone进行相应操作 
IF($IsGone) { 
$RecordDate = $DateNow; 
$CounterToday = 0; 
$CounterLastDay = $row[&#39;CounterToday&#39;]; 
$upd_sql = "update mycounter set RecordDate = &#39;$RecordDate&#39;,CounterToday = &#39;$CounterToday&#39;,CounterLastDay = &#39;$CounterLastDay&#39; WHERE id = Ƈ&#39; "; 
mysql_query($upd_sql); 
} 
//再次获取数据 
$querysql = "SELECT * FROM `mycounter` WHERE id = Ƈ&#39; "; 
$queryset = mysql_query($querysql); 
$Counter = $row[&#39;Counter&#39;]; 
$CounterToday = $row[&#39;CounterToday&#39;]; 
$CounterLastDay = $row[&#39;CounterLastDay&#39;]; 
if($row = mysql_fetch_array($queryset) ){ 
if( $_COOKIE["user"] != "oldGuest" ){ 
$Counter = ++$row[&#39;Counter&#39;]; 
$CounterToday = ++$row[&#39;CounterToday&#39;]; 
$upd_sql = "update mycounter set counter = &#39;$Counter&#39;,CounterToday = &#39;$CounterToday&#39; WHERE id = Ƈ&#39; "; 
$myquery = mysql_query($upd_sql); 
} 
echo "总访问量:".$Counter; 
echo " 
"; 
echo "今日流量:".$CounterToday; 
echo " 
"; 
echo "昨日流量:".$CounterLastDay; 
}else{//如果数据库为空时,相应的操作 
} 
} 
?>

當然,需要在檔案第一行開始寫出如下程式碼:

<?PHP 
session_start(); 
if( !isset($_COOKIE["user"]) ){ 
setcookie("user","newGuest",time()+3600); 
}else { 
setcookie("user","oldGuest"); 
} 
?>

推薦學習:《PHP影片教學

以上是php 怎麼實現前端統計流量的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn