<?php
require("ClassDataBase.php");
/*
文件名:count.php
功能:网页计数程序
作者:感染源
时间:2007-1-12
*/
//$sql = "select etime from ipandtime where ip='$ipaddress' and id=$id";
//echo $sql;
function PageCount($id)
{
$db = new ClassDataBase;
$ipaddress = $_SERVER['REMOTE_ADDR']; //获取访问者的IP地址
$currenttime = time(); //当前访问时间
//查询当前访问IP地址是否曾经访问过本网站,获得当前访问时间
$sql = "select etime from ipandtime where ip='$ipaddress' and id=$id";
$arr = $db->QuerySQL($sql);
if(!$arr)
{
//如果不存在当前IP,则添加到数据库
$sql = "insert into ipandtime (ip,id,etime) values('$ipaddress',$id,$currenttime)";
$db->ExecuteSQL($sql);
$sql = "delete from ipandtime where etime<$currenttime-3600";
$db->ExecuteSQL($sql);
$update = TRUE;
$count = Updatecount($id,$update);
return $count;
//exit;
}
else
{
//如果存在当前IP,则更新最后登录时间
$timestamp = $arr["etime"];
$stime = $currenttime - $timestamp;
//如果两次访问间隔一个小时,则更新数据库
if($stime>3600)
{
//更新访问时间
$sql = "update ipandtime set etime=$currenttime where ip='$ipaddress'";
$db->ExecuteSQL($sql);
$sql = "delete from ipandtime where etime<$currenttime-3600";
$db->ExecuteSQL($sql);
$update = TRUE;
$count = Updatecount($id,$update);
return $count;
//exit;
}//if
$sql = "delete from ipandtime where etime<$currenttime-3600";
$db->ExecuteSQL($sql);
$update = FALSE;
$count = Updatecount($id,$update);
return $count;
}//if
$db = null;
}//function PageCount();
function Updatecount($id,$update)
{
$db = new ClassDataBase;
$sql = "select count from encount where id=$id";
$result1 = $db->QuerySQL($sql);
$count = $result1["count"];
if($update==TRUE)
{
++$count;
$sql = "update encount set count=$count where id=$id";
$db->ExecuteSQL($sql);
}//if
return $count;
//$db = NULL;
}//function Updatecount();
//$db = NULL;
?>
本文介绍了一个简单的网页访问计数系统的实现方法。该系统通过记录每个IP地址的访问时间和次数来统计页面的访问量,并且为了减少数据库负担,只记录最近一小时内的访问信息。
1885

被折叠的 条评论
为什么被折叠?



