PHP会话,用于跟踪唯一的页面浏览量 [英] PHP session for tracking unique page views

查看:68
本文介绍了PHP会话,用于跟踪唯一的页面浏览量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用会话来跟踪唯一的网页浏览量.我知道这不是一个非常可靠的方法,但是足以满足我的需求.

I want to use sessions to track unique page views. Not a very robust method, I know, but sufficient for what I want to do.

在第一页加载时,将设置会话变量,并且数据库中的字段将递增.在后续的页面浏览中,该值不会增加,因为该增加取决于未设置会话变量的情况.

On the first page load the session variable is set and a field in the database increments. On subsequent page views it does not increment, because the increment is conditional on the session variable not being set.

这是我的代码:

$pagenumber = 1;

//other stuff here...

session_start();

if (!isset($_SESSION[$pagenumber])) {
    $storeview = mysqli_query($dbconnect, "UPDATE tblcount SET views=views+1 WHERE id='$pagenumber'");
    $_SESSION[$pagenumber] = $pagenumber;
}
echo $_SESSION[$pagenumber];

$Recordset1 = mysqli_query($dbconnect, "SELECT views FROM tblcount WHERE id = '$pagenumber'");
$row_Recordset1 = mysqli_fetch_assoc($Recordset1);

echo "Viewed ".$row_Recordset1['views']." times";

第一个回波仅在此处进行测试.它在页面刷新时呼应该值,并且该增量在第一次运行时有效,但是视图计数在每次页面刷新时都会继续增加,这是不应该的.我不明白为什么.

The first echo is only there for testing. It echoes the value just fine on page refresh and the increment works the first time, but the view count continues to increment on every page refresh, which it shouldn't. I can't see why.

我发现了一个类似的问题: PHP:使用cookie/对特定商品的唯一访问/点击ip ,但是我在那里遇到的解决方案遇到了类似的问题.

I found a similar question: PHP: Unique visits/hits to specific items using cookies/ip but I ran into a similar issue with the solution offered there.

帮助表示赞赏!

推荐答案

问题:

  1. 您要在每个时间tblCount中进行更新,因为每次脚本结束时,会话都将关闭. 因此:在代码中将session_start()呼叫作为第一行.
  2. 不允许将整数设置为$_SESSION变量.因此,如果设置$_SESSION[$pagenumber] = 'something',则将获得以下通知:
  1. You are updating in tblCount EACH time, because your session is closed each time your script finishes. SO: Put the session_start()call as the FIRST LINE in code.
  2. It's not permitted to set an integer as $_SESSION variable. So if you set $_SESSION[$pagenumber] = 'something', then you gain the following notice:

(!)注意:未知:在第0行的未知"中跳过数字键1

( ! ) Notice: Unknown: Skipping numeric key 1 in Unknown on line 0

相当...无法理解.有关详细信息,请参见此答案.

Quite... not understandable. For details see this answer.

$pagenumber作为索引添加到数组(此处为pagenumbers)中,并将该数组添加到$_SESSION变量内.不再通知.

Add your $pagenumber as index in an array (here pagenumbers) and that array inside the $_SESSION variable. No notice anymore.

session_start();

$pagenumber = 1;

if (!isset($_SESSION['pagenumbers'])) {
    $_SESSION['pagenumbers'] = array();
}

if (!isset($_SESSION['pagenumbers'][$pagenumber])) {
    updateViews($pagenumber);
    $_SESSION['pagenumbers'][$pagenumber] = $pagenumber;
}

echo 'Page number: ' . $_SESSION['pagenumbers'][$pagenumber] . '<br/>';
$views = getViews($pagenumber);
echo '<pre>Viewed ' . print_r($views, true) . ' times</pre>';

注意:我使用我的函数进行测试.它们只是替换您的数据库处理代码.

Note: I used my functions to test. They just replace your db-processing code.

这篇关于PHP会话,用于跟踪唯一的页面浏览量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆