存入數據庫: <?php header("Content-type: text/html; charset=utf-8"); //echo $_COOKIE['iptag']; date_default_timezone_set('PRC'); //目標網站url $aimUrl = $_GET['r']; //來源網站 $sourceUrl = $_GET['wangzhan']; //設置cookie標識符,目的是防止當產生cookie後用戶在點擊其他鏈接,造成的統計不精確 $cookieTag = $aimUrl.$sourceUrl; //明天零時的時間戳 $nonce_time = strtotime(date('Ymd')+1); setcookie('iptag',$cookieTag,$nonce_time); //獲得當前時間,用於數據庫查詢 $time = date('Y-m-d'); $db = new MySQLi('localhost','a','acyr','www_a_com'); 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); www.2cto.com //首先查看現在數據庫這一天有沒有這個鏈接的數據,如果沒有則創建,否則根據cookie值,來判斷IP和Pv的分別增加多少。 if ( $row = $res->fetch_assoc() ){ $pvSum = $row['pvSum'] + 1; 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="piaoliang_n" >漂亮網_頻道</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)); //賦月份的下拉框 for (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,