首页 >后端开发 >PHP问题 >php 怎么实现前端统计流量

php 怎么实现前端统计流量

藏色散人
藏色散人原创
2021-12-17 09:45:372278浏览

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