duandi8613 2014-07-14 20:05 采纳率: 100%
浏览 27

我的页面查看计数器在我的主页上增加了1个单位,但在次要页面上增加了3个单位

Im doing a function to count visitors, views and page_views.

For now Im just trying to count page views but Im having a strange issue.

I have a table stats with this columns: "users,visits,page_views".

And now I just want to increment my page_views when user acess some page.

Every time I acess my homepage my page_views is incremented 1 unit, but when I acess a secundary page my column page_views is incremented with 3 units, and I want just also 1 unit.

Do you know why this can be happening?

I think that problem is in my getHome() function, because If I call my function countViews() before or after my getHome(), I have different behaviors. But Im not finding why.

When I have me countViews() above my getHome(), It seems that Im calling 3 times my function countViews on my secundary pages.

But I only call my function countViews() on my index.php file and below this function I have my getHome() function where I do my navigation to all pages I have in my project.

function countViews(){
    $pdo = start();
    //if Im not logged as admin
    if(!isset($_SESSION['admin'])){
        //So at first I read my table stats, if my table dont have any row,
        // I create a row with my columns with value of '0'.
        $readStats= $pdo->prepare("SELECT * FROM stats"); 
        $readStats->execute();  

        if($readStats->rowCount()<1){
            $insStats= $pdo->prepare("INSERT INTO stats(users, views, page_views) VALUES (?,?,?)");
            $insStats->bindValue(1,'0');
            $insStats->bindValue(2,'0');
            $insStats->bindValue(3,'0');
            $insStats->execute();
        }
       //If my table stats already have one row, I want to increment page_views.  
        else{
            $resultStats= $readStats->fetch(PDO::FETCH_ASSOC);
            $incrementPageViews = $resultStats['page_views'] + 1;
            $updatePageViews =  $pdo->prepare("UPDATE stats set page_views = ?");
            $updatePageViews ->bindParam(1,$incrementPageViews);
            $updatePageViews ->execute();
        }

    }
}

My index.php is like:

<?php
  ob_start(); session_start();
  require_once('dts/db.php');
  $pdo = start();
  countViews();
 ?>
 <!DOCTYPE>
 <!-- here I have my css and js imports -->

<?php getHome(); ?>

My secundary pages:

<title>Untitled Document</title>
 </head> 
 <body>
 <!-- here I have my html -->
 </body>

getHome() function:

<?php
function getHome(){
    $url = $_GET['url'];
    $url = explode('/', $url);
    $url[0] = ($url[0] == NULL ? 'index' : $url[0]);

    if(file_exists('project/'.$url[0].'.php')){
        require_once('project/'.$url[0].'.php');
    }
    elseif(file_exists('project/'.$url[0].'/'.$url[1].'.php')){
        require_once('project/'.$url[0].'/'.$url[1].'.php');
    }
    else{
        require_once('project/404.php');
    }
}

?>

My table is like:

enter image description here

  • 写回答

5条回答 默认 最新

  • dtxooq1020 2014-07-16 23:27
    关注

    There're two possibilities I can think of.

    1. You have countViews in one of your require_once files
    2. You have some redirect code in your secondary pages.

    What I'm actually saying is, I'm sure it's not magic.

    评论

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器