一般目标网站的网址连接是这样的样式:
http://localhost/tongji/url.php?wangzhan=piaoliang&r=http://www.taobao.com/
存入数据库:
<?php header("Content-type: text/html; charset=utf-8"); date_default_timezone_set('PRC'); //目标网站url $aimUrl = $_GET['r']; //来源网站 $sourceUrl = $_GET['wangzhan']; //设置cookie标识符,目的是防止当生产cookie后用户在点击其他链接,造成的统计不精确 $cookieTag = $aimUrl.$sourceUrl; setcookie('iptag',$cookieTag,time()+60); //当前日期 $time = date('Y-m-d'); $db = new MySQLi('127.0.0.1','root','1','tongji'); if ($mysqli->connect_errno) { die('数据库连失败:'.$mysqli->connect_error); } $db->query('set names utf8'); $sql = "select * from dede_tongji where sourceUrl='$sourceUrl' and aimUrl='$aimUrl' and date='$time'"; $res = $db->query($sql); //首先查看现在数据库这一天有没有需要这个链接的数据,如果没有则创建,否则根据cookie值,来判断IP和Pv的分别增加多少。 if ( $row = $res->fetch_assoc() ){ $pvSum = $row['pvSum'] + 1; //如果 cookie中的值是 $cookieTag 说明是已经存在的 所以pv加一 if( $_COOKIE['iptag'] == $cookieTag ){ $sql = "update dede_tongji set pvSum = '$pvSum' where sourceUrl='$sourceUrl' and aimUrl='$aimUrl' and date='$time' "; $db->query($sql); //否则只是ip 和pv 增加一 }else{ $ipSum = $row['ipSum'] + 1; $sql = "update dede_tongji set ipSum = '$ipSum',pvSum = '$pvSum' where sourceUrl='$sourceUrl' and aimUrl='$aimUrl' and date='$time' "; $db->query($sql); } }else{ //数据库中没有则添加一条新数据 $sql = "insert into dede_tongji (sourceUrl,aimUrl,ipSum,pvSum,date) values ('$sourceUrl','$aimUrl',1,1,'$time')"; if( $db->query($sql)){ }else{ $db->error; } } //js实现跳转 echo "<script language='javascript'>location.href='".$aimUrl."'</script>"; ?>
从数据库中查询,这里使用到了一个分页类在我的令一片博客里大家可以找到,关于这个分页类的用法 结合这个例子和分页类的成员函数可以很好的理解,主要是在进行数据库查询时sql语句带上 limit 限定条件就可以了:page.class.php
下面是从数据库中查询前台显示数据和分页类的使用,还有一些控制日期的js
<?php header("Content-type: text/html; charset=utf-8"); date_default_timezone_set('PRC'); //引入分页类 require_once 'page.class.php'; //获取变量 $wangzhan = emptyempty($_GET['wangzhan']) ? '' : $_GET['wangzhan']; //如果月份和日期小于10则加0 用于数据库日期匹配 $mm = $_GET['MM']; $dd = $_GET['DD']; if( $mm < 10 ){ $mm = '0'.$mm; } if( $dd < 10 ){ $dd = '0'.$dd; } $date = $_GET['YYYY'].'-'.$mm.'-'.$dd; //echo $date; $time = date('Y-m-d'); echo 'time:'.$time.'<br>'; echo $date; //exit(); //连接数据库 $db = new MySQLi('localhost','a','acyr','www_a_com'); if ($mysqli->connect_errno) { die('数据库连失败:'.$mysqli->connect_error); } $db->query('set names utf8'); //如果现在的查询日期是当前日期,则全部输出且按日期排序 if($date==$time){ //获得这种情况下的总条数,用于分页显示(分页类要用到这个参数) $sql = "select count(*) from dede_tongji where sourceUrl='$wangzhan' order by date desc"; $row = $db->query($sql)->fetch_row(); $allRows = $row[0]; //总条数 $pageList = new Page($allRows,2,4,array('pre'=>'上一页','next'=>'下一页')); //$res = $db->query( "select * from dede_tongji where sourceUrl='$wangzhan' order by date desc {$pageList->limit()}" ); $sql = "select * from dede_tongji where sourceUrl='$wangzhan' order by date desc {$pageList->limit()}"; //echo $sql; $res = $db->query($sql); $resArr = array(); while( $row = $res->fetch_assoc()){ $resArr[] = $row; } $res->free_result(); /*echo '<pre>'; var_dump($resArr); foreach( $resArr as $v ){ echo $v['aimUrl']; }*/ //print_r($res); //exit(); }else{ //获得这种情况下的总条数,用于分页显示 $sql = "select count(*) from dede_tongji where sourceUrl='$wangzhan' and date='$date'"; $row = $db->query($sql)->fetch_row(); $allRows = $row[0]; //总条数 $pageList = new Page($allRows,2,4,array('pre'=>'上一页','next'=>'下一页')); $sql = "select * from dede_tongji where sourceUrl='$wangzhan' and date='$date' {$pageList->limit()} "; //echo $sql; $res = $db->query($sql); $resArr = array(); while( $row = $res->fetch_assoc()){ $resArr[] = $row; } $res->free_result(); //print_r($res); //exit(); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>统计结果</title> <style type="text/css"> /*头部样式*/ .top { margin-right: auto; margin-left: auto; width: 800px; margin-top: 100px; } /*表格样式*/ table td{ border:1px solid #999; padding:0px 5px; } /*分页样式*/ .pagelist{ margin-right: auto; margin-left: auto; width: 800px; margin-top: 30px; } .pagelist a{ text-decoration:none; display:block; height:auto; width:auto; float:left; padding:1px 6px; color:#333; margin-right:5px; text-align:center; border:1px solid #CCC; } .pagelist a:hover{ color:#F63; } .pagelist .alink{ text-align:center; width:20px; } .pagelist strong{ text-decoration:none; display:block; float:left; text-align:center; width:20px; padding:1px 6px; border:1px solid #CCC; margin-right:5px; color:#FFF; background:#666; } .pagelist .sel{ width:40px; } </style> </head> <body> <div class="top"> <!--下拉框固定焦点--> <script> <!-- function fix(){ //alert('ok!'); document.getElementById('wangzhan').options.focus(); } //--> </script> <form method="get" action="tongji.php" style="margin-top:10px;" name='form1'> 统计网站:<select name="wangzhan" id='wangzhan' onchange="fix()"> <option value="">--请选择--</option> <option value="piaoliang" >漂亮网</option> <option value="bohe">薄荷网</option> <option value="jianfen">减肥网</option> <option value="tianqi">天气网</option> <option value="nvsheng">女生网</option> </select> 查询日期: <select name='YYYY' onChange="YYYYDD(this.value)"> <option value="" selected='selected'>请选择 年</option> </select> <select name='MM' onChange="MMDD(this.value)"> <option value="">选择 月</option> </select> <select name='DD'> <option value="">选择 日</option> </select> <input type="submit" value="查询" /> </form> </div> <table width="800" align="center" cellpadding="0" cellspacing="0" style="margin-top:20px; border:1px solid #999;"> <tr style="border:1px solid #999;"> <td width="140" height="25" align="center" bgcolor="#999999" >统计网站</td> <td width="240" align="center" bgcolor="#999999">出站的连接</td> <td width="140" align="center" bgcolor="#999999">IP(独立)</td> <td width="140" align="center" bgcolor="#999999">PV</td> <td width="140" align="center" bgcolor="#999999">日期</td> </tr> <?php foreach ( $resArr as $v ){ ?> <tr align="center"> <td height="25" ><?php echo $v['sourceUrl'];?></td> <td><?php echo $v['aimUrl']?></td> <td><?php echo $v['ipSum']?></td> <td><?php echo $v['pvSum']?></td> <td><?php echo $v['date']?></td> </tr> <?php }?> </table> <div class="pagelist"> <?php if( $wangzhan != '' && isset($resArr[0])){ echo $pageList->pre(); echo $pageList->first();echo $pageList->strList(); echo $pageList->end();echo $pageList->next(); echo " 请选择跳转到第: ";echo $pageList->selectList().' 页'; } ?> </div> <script language="JavaScript"> <!-- function YYYYMMDDstart() { MonHead = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; //先给年下拉框赋内容 var y = new Date().getFullYear(); for (var i = (y-3); i < (y+1); i++) //以今年为准,前30年,后30年 document.form1.YYYY.options.add(new Option(" "+ i +" 年", i)); //赋月份的下拉框 www.it165.netfor (var i = 1; i < 13; i++) if( i < 10 ){ /*如果是小于10的数,则加上0和日期匹配,下面的天数是一样*/ document.form1.MM.options.add(new Option("0" + i + " 月", i)); }else{ document.form1.MM.options.add(new Option("" + i + " 月", i)); } document.form1.YYYY.value = y; document.form1.MM.value = new Date().getMonth() + 1; var n = MonHead[new Date().getMonth()]; if (new Date().getMonth() ==1 && IsPinYear(YYYYvalue)) n++; writeDay(n); //赋日期下拉框Author:meizz document.form1.DD.value = new Date().getDate(); } if(document.attachEvent) window.attachEvent("onload", YYYYMMDDstart); else window.addEventListener('load', YYYYMMDDstart, false); function YYYYDD(str) //年发生变化时日期发生变化(主要是判断闰平年) { var MMvalue = document.form1.MM.options[document.form1.MM.selectedIndex].value; if (MMvalue == ""){ var e = document.form1.DD; optionsClear(e); return;} var n = MonHead[MMvalue - 1]; if (MMvalue ==2 && IsPinYear(str)) n++; writeDay(n) } function MMDD(str) //月发生变化时日期联动 { var YYYYvalue = document.form1.YYYY.options[document.form1.YYYY.selectedIndex].value; if (YYYYvalue == ""){ var e = document.form1.DD; optionsClear(e); return;} var n = MonHead[str - 1]; if (str ==2 && IsPinYear(YYYYvalue)) n++; writeDay(n) } function writeDay(n) //据条件写日期的下拉框 { var e = document.form1.DD; optionsClear(e); for (var i=1; i<(n+1); i++) if( i < 10 ){ e.options.add(new Option("0"+ i + " 日", i)); }else{ e.options.add(new Option(""+ i + " 日", i)); } } function IsPinYear(year)//判断是否闰平年 { return(0 == year%4 && (year%100 !=0 || year%400 == 0));} function optionsClear(e) { e.options.length = 1; } //--></script> </body> </html>
最后还有就是数据库表的结构,这样只有一张表 在pv记录上还有一些小误差,因为这里主要是用 cookie控制的时间,所以有误差 如果有高手可以优化数据库设计用ip来控制,能力有限,望大家见谅:
CREATE TABLE `dede_tongji` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `sourceUrl` varchar(255) NOT NULL, `aimUrl` varchar(255) NOT NULL, `ipSum` int(10) unsigned NOT NULL, `pvSum` int(10) unsigned NOT NULL, `date` date NOT NULL,PHP编程
郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。
新闻热点
疑难解答