php流量统计功能的实现代码
程序员文章站
2022-04-19 23:17:59
流量统计功能 显示效果: 总访问量:399 今日流量:14 昨日流量:16 本代码仅供学习交流,其中必有不妥之处。请见谅! -- -- 表的结构 `mycounter` -...
流量统计功能
显示效果:
总访问量: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 = Ƈ' ";
$queryset = mysql_query($querysql);
$row = mysql_fetch_array($queryset);
//获得时间量
$datenow = date('y-m-d');
$recorddate = $row['recorddate'];
$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;
}else break;
//根据isgone进行相应操作
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);
}
//再次获取数据
$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 "总访问量:".$counter;
echo "
";
echo "今日流量:".$countertoday;
echo "
";
echo "昨日流量:".$counterlastday;
}else{//如果数据库为空时,相应的操作
}
}
?>
当然,需要在文件第一行开始写出如下代码:
<?php
session_start();
if( !isset($_cookie["user"]) ){
setcookie("user","newguest",time()+3600);
}else {
setcookie("user","oldguest");
}
?>
显示效果:
总访问量: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 = Ƈ' ";
$queryset = mysql_query($querysql);
$row = mysql_fetch_array($queryset);
//获得时间量
$datenow = date('y-m-d');
$recorddate = $row['recorddate'];
$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;
}else break;
//根据isgone进行相应操作
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);
}
//再次获取数据
$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 "总访问量:".$counter;
echo "
";
echo "今日流量:".$countertoday;
echo "
";
echo "昨日流量:".$counterlastday;
}else{//如果数据库为空时,相应的操作
}
}
?>
当然,需要在文件第一行开始写出如下代码:
复制代码 代码如下:
<?php
session_start();
if( !isset($_cookie["user"]) ){
setcookie("user","newguest",time()+3600);
}else {
setcookie("user","oldguest");
}
?>
下一篇: python之打印九九乘法表