PHP网页计数器

本文介绍了一个简单的网页访问计数系统的实现方法。该系统通过记录每个IP地址的访问时间和次数来统计页面的访问量,并且为了减少数据库负担,只记录最近一小时内的访问信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

<?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;

?> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值