当前位置:首页> PHP教程> php基础
关键字
文章内容
简易流量统计范例
 
 
修改时间:[2011/05/09 08:37]    阅读次数:[893]    发表者:[起缘]
 
以下为本站的简易流量统计范例,预设为只显示当天流量及总次数,若要查阅前几天的请在url後面加上?day=天数,如要查阅前2天,则输入http://ajax.seed.cx/status.php?day=2,另外,此流量只有首页被点阅才计次,同ip则只在那个ip的次数加1。
备注:以下为利用MySQL来统计流量,其table格式如下:
create table stream (ip TEXT,sTime INT,visits INT,info TEXT)
统计部分程式码(放在首页)
<?
$mlink=mysql_connect('localhost','ajax','xxxxxxx');
$sip=getenv("REMOTE_ADDR");
$sinfo=getenv("HTTP_USER_AGENT");
$stoday=mktime(0,0,0,date(m),date(d),date(Y));
$query="select * from stream where ip='".$sip;
$query.="' and sTime>=".$stoday;
$result=mysql_db_query('ajax',$query,$mlink);
//判断今天有无来过,有则增加拜访次数1,无则新增资料
if($row=mysql_fetch_object($result)){
$row->visits++;
$query="update stream set visits=".$row->visits.", sTime=";
$query.=time()." where ip='".$sip."' and sTime>=".$stoday;
$result=mysql_db_query('ajax',$query,$mlink);
}else{
$query="insert into stream values('".$sip."',".time();
$query.=",1,'".$sinfo."')";
$result=mysql_db_query('ajax',$query,$mlink);
}
mysql_close($mlink);
?>
显示部分程式码及HTML
<head>
<title>流量统计</title>
<style>
<!--
.st1 { color: .st2 { font-size: 10pt; color: .st3 { font-size: 10pt }
.st4 { font-size: 10pt; color: -->
</style>
</head>
<?
if($day<0)$day=0;
$mlink=mysql_connect('localhost','ajax','xxxxxxx');
//统计至今首页被浏览的总次数
$query="select SUM(visits) as tvisits from stream";
$result=mysql_db_query('ajax',$query,$mlink);
echo '<span class="st1">浏览首页总累积次数:';
if($row=mysql_fetch_object($result)){
echo $row->tvisits;
}
$week=array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
for($i=0;$i<=$day;$i++){
echo '<p>';
$ctime1=time()-24*3600*$i;
$ctime2=time()-24*3600*($i-1);
echo '<span class="st4">日期:'.date("m月d日",$ctime1).$week[date("w",$ctime1)];
echo "</span><br>n";
$stoday1=mktime(0,0,0,date(m,$ctime1),date(d,$ctime1),date(Y,$ctime1));
$stoday2=mktime(0,0,0,date(m,$ctime2),date(d,$ctime2),date(Y,$ctime2));
//统计当日首页被浏览的累积次数
$query='select SUM(visits) as tvisits from stream where sTime>=';
$query.=$stoday1.' and sTime<'.$stoday2;
$result=mysql_db_query('ajax',$query,$mlink);
echo '<span class="st3">浏览首页累积次数:';
if($row=mysql_fetch_object($result)){
echo $row->tvisits;
}
//统计当日拜访ip的数量
$query='select * from stream where sTime>='.$stoday1;
$query.=' and sTime<'.$stoday2.' order by sTime DESC';
$result=mysql_db_query('ajax',$query,$mlink);
echo "<br>浏览总人数:".mysql_num_rows($result);
echo '</span><hr size="1">';
echo '<table border="0" width="100%" cellspacing="0" cellpadding="2" class="st3">';
echo "n";
echo '<tr><td nowrap class="st2">IP位址</td>
<td nowrap class="st2">最近时间</td>
<td nowrap class="st2">次数</td>
<td nowrap class="st2">用户资讯</td>';
echo "</tr>n";
while($row=mysql_fetch_object($result)){
echo '<tr>';
echo '<td nowrap>'.$row->ip.'</td>';
echo '<td nowrap>'.date("h:i:sa",$row->sTime).'</td>';
echo '<td nowrap>'.$row->visits.'</td>';
echo '<td nowrap>'.$row->info.'</td>';
echo "</tr>n";
}
echo "n</table>";
echo '<hr size="1">';
}
mysql_close($mlink);
?>
作者:佚名
phpfans.net收集整理