php实现从本网站每天出站连接向目标网站贡献的IP和PV的统计
存入数据库:
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 "";
?>
从数据库中查询,这里使用到了一个分页类在我的令一片博客里大家可以找到,关于这个分页类的用法 结合这个例子和分页类的成员函数可以很好的理解,主要是在进行数据库查询时sql语句带上 limit 限定条件就可以了,用到的分页类是:page.class.php
下面是从数据库中查询前台显示数据和分页类的使用,还有一些控制日期的js
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
$mm = '0'.$mm;
}
if( $dd
$dd = '0'.$dd;
}
$date = $_GET['YYYY'].'-'.$mm.'-'.$dd;
//echo $date;
//$time = date('Y-m-d');
//echo 'time:'.$time.'
';
//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 '
'; <p> var_dump($resArr); </p> <p> foreach( $resArr as $v ){ </p> <p> echo $v['aimUrl']; </p> <p> }*/ </p> <p> //print_r($res); </p> <p> //exit(); </p> <p> }else{ </p> <p> //获得这种情况下的总条数,用于分页显示 </p> <p> $sql = "select count(*) from dede_tongji where sourceUrl='$wangzhan' and date='$date'"; </p> <p> $row = $db->query($sql)->fetch_row(); </p> <p> $allRows = $row[0]; //总条数 </p> <p> $pageList = new Page($allRows,2,4,array('pre'=>'上一页','next'=>'下一页')); </p> <p> $sql = "select * from dede_tongji where sourceUrl='$wangzhan' and date='$date' {$pageList->limit()} "; </p> <p> //echo $sql; </p> <p> $res = $db->query($sql); </p> <p> $resArr = array(); </p> <p> while( $row = $res->fetch_assoc()){ </p> <p> $resArr[] = $row; </p> <p> } </p> <p> $res->free_result(); </p> <p> //print_r($res); </p> <p> //exit(); </p> <p> } </p> <p> </p> <p>?> </p> <p> </p> <p> </p> <p></p> <p><meta http-equiv="Content-Type" content="text/html; charset=utf-8"> </p> <p></p><title>统计结果</title> <p><style type="text/css"> </style></p> <p>/*头部样式*/ </p> <p>.top { </p> <p> margin-right: auto; </p> <p> margin-left: auto; </p> <p> width: 800px; </p> <p> margin-top: 100px; </p> <p>} </p> <p>/*表格样式*/ </p> <p>table td{ </p> <p> border:1px solid #999; </p> <p> padding:0px 5px; </p> <p>} </p> <p> </p> <p>/*分页样式*/ </p> <p>.pagelist{ </p> <p> margin-right: auto; </p> <p> margin-left: auto; </p> <p> width: 800px; </p> <p> margin-top: 30px; </p> <p>} </p> <p>.pagelist a{ </p> <p> text-decoration:none; </p> <p> display:block; </p> <p> height:auto; </p> <p> width:auto; </p> <p> float:left; </p> <p> padding:1px 6px; </p> <p> color:#333; </p> <p> margin-right:5px; </p> <p> text-align:center; </p> <p> border:1px solid #CCC; </p> <p>} </p> <p>.pagelist a:hover{ </p> <p> color:#F63; </p> <p>} </p> <p>.pagelist .alink{ </p> <p> text-align:center; </p> <p> width:20px; </p> <p>} </p> <p>.pagelist strong{ </p> <p> text-decoration:none; </p> <p> display:block; </p> <p> </p> <p> </p> <p> float:left; </p> <p> </p> <p> text-align:center; </p> <p> width:20px; </p> <p> padding:1px 6px; </p> <p> border:1px solid #CCC; </p> <p> margin-right:5px; </p> <p> color:#FFF; </p> <p> background:#666; </p> <p>} </p> <p>.pagelist .sel{ </p> <p> width:40px; </p> <p>} </p> <p> </p> <p> </p> <p> </p> <p></p> <p></p><div class="top"> <p><!--下拉框固定焦点--> </p> <p><script> </script></p> <p><!-- </p> <p> function fix(){ </p> <p> //alert('ok!'); </p> <p> document.getElementById('wangzhan').options.focus(); </p> <p> } </p> <p>//--> </p> <p> </p> <p></p> <form method="get" action="tongji.php" style="margin-top:10px;" name="'form1'"> <p>统计网站:<select name="wangzhan" id="'wangzhan'" onchange="fix()"> </select></p> <p> <option value="">--请选择--</option> </p> <p> <option value="piaoliang">漂亮网</option> </p> <p> <option value="piaoliang_n">漂亮网_频道</option> </p> <p> <option value="bohe">薄荷网</option> </p> <p> <option value="jianfen">减肥网</option> </p> <p> <option value="tianqi">天气网</option> </p> <p> <option value="nvsheng">女生网</option> </p> <p> </p> <p> 查询日期: </p> <p> <select name="'YYYY'" onchange="YYYYDD(this.value)"> </select></p> <p> <option value="" selected>请选择 年</option> </p> <p> </p> <p> <select name="'MM'" onchange="MMDD(this.value)"> </select></p> <p> <option value="">选择 月</option> </p> <p> </p> <p> <select name="'DD'"> </select></p> <p> <option value="">选择 日</option> </p> <p> </p> <p> </p> <p> </p> <p> <input type="submit" value="查询"> </p> <p></p> </form> <p></p> </div> <p></p>
统计网站 | 出站的连接 | IP(独立) | PV | 日期 |
if( $wangzhan != '' && isset($resArr[0])){
echo $pageList->pre(); echo $pageList->first();echo $pageList->strList();
echo $pageList->end();echo $pageList->next();
echo " 请选择跳转到第: ";echo $pageList->selectList().' 页';
}
?>
最后还有就是数据库表的结构,这样只有一张表 在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,

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
