Maison  >  Article  >  développement back-end  >  php实现从本网站每天出站连接向目标网站贡献的IP和PV的统计_PHP教程

php实现从本网站每天出站连接向目标网站贡献的IP和PV的统计_PHP教程

WBOY
WBOYoriginal
2016-07-14 10:07:241056parcourir

存入数据库:

 
    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 '
';  
        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();  
    }  
 
?>  
 
 
 
 
统计结果  
/*头部样式*/ 
.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;  
}  
 
 
 
 
 
 
<script> </script>
 
 
 
统计网站:
                 
                 
                 
                 
                 
                 
                             
                   
    查询日期:  
           
              
              
           
              
              
           
              
              
 
 
     
 
 
 
 
 
   
 
   
 
   
 
   
 
   
 
 
 
 
 
 
   
 
   
 
   
 
   
 
   
 
 
 
   
统计网站 出站的连接 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, 
 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/477856.htmlTechArticle存入数据库: ?php header(Content-type: text/html; charset=utf-8); //echo $_COOKIE[iptag]; date_default_timezone_set(PRC); //目标网站url $aimUrl = $_GET[r]; //来源网站...
Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Article précédent:关于ob_start()_PHP教程Article suivant:php上传文件类_PHP教程