如何跟踪用户在网站上的使用情况?

发布于 2024-12-29 09:29:53 字数 445 浏览 2 评论 0原文

我正在使用 PHP/MYSQL。每次用户登录时,我们都会插入他们登录的时间和登录日期。虽然我们很容易知道特定一天有多少用户登录,但我不确定如何计算他们在网站上花费的时间。

例如,就像用户登录并将记录插入到 login_tracking 表中一样,当用户点击“注销”时,我们也会更新该记录以及注销时间。这样,我们减去登录和注销的时间,就可以得到每个人花费的时间,但是如果大多数人没有点击“注销”,而是清除 cookie/缓存或只是关闭网站打开的窗口/选项卡,会发生什么在?

有更好的方法吗?我正在使用 Google Analytics,它为我提供每天都会出现的用户和访问,但我想要一些专有的东西来跟踪使用情况?

有什么想法我能做什么吗?

更新:@Paul 问我是否正在使用 Sessions。我就是这样,这就是我使用它的方式:

$username = $_SESSION["username"];

I am using PHP/MYSQL. Each time a user logs in we insert the time they logged in and the date they logged in. While its easy for us to tell how many users logged in a particular day i am not sure how to calculate how much time they spent on the site.

For instance, just like a user signs in and we insert record into login_tracking table we also update that record when the user hits "logout" with the time they logged out. That way we subtract the time logged in and logged out and we get each persons time spent, but what happens if majority of people don't hit "logout" and instead clear cookies/cache or just close the window/tab the site was open in?

Is there a better way to do this? I am using Google Analytics, which gives me users and visits that showed up everyday but i want something proprietary to track usage?

Any ideas what i can do?

Update: @Paul asked if i was using Sessions. I am and this is how i am using it:

$username = $_SESSION["username"];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

静若繁花 2025-01-05 09:29:53

您可以通过在他们登录时设置日期时间来完成此操作。

因此,当用户登录时,您会更新 mysql 字段,即 DATETIME,类似于...

 $q = $dbc -> prepare("UPDATE accounts SET loggedOn = NOW() WHERE username = ?");
 $q -> execute(array($_SESSION['username']));

现在您所要做的就是在数据库表中再创建几列,其中一个名为 totalTime,还有一个名为 active 或任何您喜欢的名称。

在每个页面上加载更新active,它是一个DATETIME列,也带有NOW()

 $q = $dbc -> prepare("UPDATE accounts SET active = NOW() WHERE username = ?");
 $q -> execute(array($_SESSION['username']));

这确保您知道用户是否仍然在线无论是否,您都可以检查它们上次活动的时间。现在假设,如果在 5 分钟不活动后,您想找到您简单执行的总时间,

 $q = $dbc -> prepare("UPDATE accounts SET totalTime = ADDDATE(totalTime, DATEDIFF(NOW(), loggedOn)) WHERE active < DATE_SUB(NOW(), INTERVAL 5 MINUTE)");
 $q -> execute();

上面的作用是,如果用户不活动 5 分钟,则将使用 loggedOn 时间减去当前时间,留下网站上的总时间并将其添加到 DATETIMEtotalTime 中。

只需将最后一个查询设置为每 5 分钟一次的 cron 集,您就可以非常准确地测量人们在您网站上的时间。

You can do this by setting a date time at the point of when they log on.

So when a user log on you update the mysql field which is DATETIME, something like...

 $q = $dbc -> prepare("UPDATE accounts SET loggedOn = NOW() WHERE username = ?");
 $q -> execute(array($_SESSION['username']));

Now all you would have to do is create a few more columns in your database table one called totalTime, and one called active or whatever you like upto you really.

On every page load the update active which is a DATETIME column also with NOW(),

 $q = $dbc -> prepare("UPDATE accounts SET active = NOW() WHERE username = ?");
 $q -> execute(array($_SESSION['username']));

This ensures that you know if the user is still online or not, you can check when they were last active. Now say if after 5 minutes of inactivity you want to find the total time you simply do,

 $q = $dbc -> prepare("UPDATE accounts SET totalTime = ADDDATE(totalTime, DATEDIFF(NOW(), loggedOn)) WHERE active < DATE_SUB(NOW(), INTERVAL 5 MINUTE)");
 $q -> execute();

What the above does is if users inactive for 5 minutes, it will take the loggedOn time minus it from the current time, leaving you with the total time on the site and add it to the DATETIME column totalTime.

Simply put the last query as a cron set too every 5 minutes and you have a quite accurate measure of peoples time on your site.

一枫情书 2025-01-05 09:29:53

我建议您使用“上次使用”字段代替保存注销日期/时间的字段,使用“首次使用”字段代替保存登录日期/时间的字段。

每当用户请求页面时,您将检查“上次使用”值,如果是不久之前(即:例如不到 15 分钟),您将其更新为当前日期/时间,否则,您将获得在数据库中创建一条新记录(假设用户现在处于新的“会话”中,但不必创建新记录)。当然,还将会话与新创建的记录相关联。

这是一个伪代码:

Page Requested
if new session
    create session 
    create database record (first used=now, last used = now)
    associate the session with the record
else
    query the record associated with the current session
    if long ago
        create a new record (first used=now, last used = now)
        associate the new record with the current session
    else
        update the existing record (last used = now)

看起来很简单,但我没有尝试实现它。

I suggest you use a "last used" field instead the field that holds the logout date/time, and a "first used" field instead of the field that holds the login date/time.

Whenever the user requests a page, you will check the "last used" value, if it was not long ago (i.e: less than 15 minutes for example), you will update it to the current date/time, else, you will have to create a new record in the database (assume that the user is in a new "session" right now, but don't have to create a new one). And, of course, associate the session with the newly created record.

This is a pseudo code:

Page Requested
if new session
    create session 
    create database record (first used=now, last used = now)
    associate the session with the record
else
    query the record associated with the current session
    if long ago
        create a new record (first used=now, last used = now)
        associate the new record with the current session
    else
        update the existing record (last used = now)

It looks easy, I didn't try to implement it though.

财迷小姐 2025-01-05 09:29:53

我制作了一个简单的网站聊天脚本,执行类似的操作。

我所做的是将他们的会话 ID 和时间戳存储在表中。他们访问的每个页面,我都会更新时间戳。当他们注销时,我销毁他们的日志。我还检查了 5 分钟内“不活动”的其他用户的更新脚本并将其删除。您可以通过设置一个 cron 作业每 5 或 10 分钟运行一次并检查那些早于 5-10 分钟的时间戳并将这些用户标记为不活动,然后计算在网站上花费的总时间来完成同样的事情。

如果您只想统计在特定时间活跃的用户数量,我会查看 http://whos.amung.us/ 。它是一个简单的插件,可以计算任何给定时间的在线人数以及他们来自哪里。

I made a simple website chat script doing something kind of like this.

What I did was store their session id and the time stamp in a table. Every page they visit, I would update the timestamp. When they log out, I destroy their log. I also had the update script check for other users that were "inactive" for 5 minutes and deleted them. You could accomplish the same thing by setting up a cron job to run every 5 or 10 minutes and check those timestamps which are older than 5-10 minutes and mark those users as inactive and then calculate total time spent on the site.

If you just want a count of users active at a specific time, I would look into http://whos.amung.us/. Its a simple plugin that counts the number of people online at any given time and where they are coming from.

听你说爱我 2025-01-05 09:29:53

调整您的login_tracking表以包括:

  • session_id(varchar 100)
  • user_id
  • 时间(只要有活动,每10分钟更新一次)
  • 开始(日期时间,当他们第一次登录时)
  • 结束(日期时间,当会话被关闭的浏览器或终止时)
    logout)

代码:

$date = date('Y-m-d H:i:s');
$time=time();
$time_check=$time-600; // user will be marked online if they have any activity for 10 minutes
$session=session_id();

// check if the user is currently listed as online in the database
$getactiveession = mysql_query("SELECT * FROM login_tracking WHERE session = '$session' AND end IS NULL");
$active_session = mysql_num_rows($getactivesession);

// if they are not listed as online, insert a row for them
if($active_session == "0"){
$make_active = mysql_query("INSERT INTO login_tracking (session, user_id, time, start) VALUES('$session', '$user', '$time', '$date')");
}
else {
// UPDATE user's current time if they are already currently logged in
$update_session = mysql_query("UPDATE login_tracking SET time = '$time', user_id = '$user' WHERE session = '$session'");
}

// get total users online
$gettotalonline = mysql_query("SELECT * FROM login_tracking");
$total_online = mysql_num_rows($gettotalonline);


// if over 10 minutes and no new activity (time column has not been updated), update end session time
$end_session_time = mysql_query("UPDATE login_tracking SET end = '$date' WHERE time<$time_check");

使用此代码,您可以显示所有在线用户的列表以及其个人资料、头像等的链接。

我唯一能想到的是,在为新登录用户插入新行时,您可能必须执行“WHERE end = '0000-00-00 00:00:00”而不是“WHERE end IS NULL”,不确定是否可以在日期时间上执行 IS NULL。

对您的评论 #1 的回复

我的网站上有一个类似的功能,该功能位于每个页面上,因此无论我的用户正在浏览哪个页面,他们都会被视为已登录。因此很容易各地落实。

但是,您可能需要将其一部分设置为 cronjob,每分钟或 10 分钟运行一次,以检查超过 10 分钟的记录。我这样说是因为......如果用户关闭浏览器但没有注销,则在另一个人(登录或访客)访问正在运行上述代码的页面之前,它不会更新其在数据库中的记录。如果你有一个 cronjob 运行它那么就不会有问题。 (这也解决了您关于执行结束会话时间的评论,我的网站不需要您很高兴地提出这一点,因此我认为您的 cronjob 是必须的)。
至于将它们重定向到注销页面,我不确定在这种情况下会如何工作,因为他们的页面没有被刷新。那可能需要 ajax 之类的。

Adjust your login_tracking table to include:

  • session_id (varchar 100)
  • user_id
  • time (updates every 10 minutes for as long as there is activity)
  • start (datetime, when they first login)
  • end (datetime, when the session is killed either by closed browser or
    logout)

Code:

$date = date('Y-m-d H:i:s');
$time=time();
$time_check=$time-600; // user will be marked online if they have any activity for 10 minutes
$session=session_id();

// check if the user is currently listed as online in the database
$getactiveession = mysql_query("SELECT * FROM login_tracking WHERE session = '$session' AND end IS NULL");
$active_session = mysql_num_rows($getactivesession);

// if they are not listed as online, insert a row for them
if($active_session == "0"){
$make_active = mysql_query("INSERT INTO login_tracking (session, user_id, time, start) VALUES('$session', '$user', '$time', '$date')");
}
else {
// UPDATE user's current time if they are already currently logged in
$update_session = mysql_query("UPDATE login_tracking SET time = '$time', user_id = '$user' WHERE session = '$session'");
}

// get total users online
$gettotalonline = mysql_query("SELECT * FROM login_tracking");
$total_online = mysql_num_rows($gettotalonline);


// if over 10 minutes and no new activity (time column has not been updated), update end session time
$end_session_time = mysql_query("UPDATE login_tracking SET end = '$date' WHERE time<$time_check");

With this you can display a list of all online users as well with links to their profile, avatars, etc.

only thing I can think of is when inserting the new row for a new logged in user, you might have to do "WHERE end = '0000-00-00 00:00:00" instead of "WHERE end IS NULL", not sure if you can do a IS NULL on a datetime.

Response to your Comment #1

I have a SIMILAR thing as a function on my site which is located on every page therefor no matter what page my user is browsing they will be considered logged in. So it's easy to implement everywhere.

However, you will probably need to set part of it up as a cronjob to run every minute or 10 minutes to check for records over 10 minutes. I say this because...if a user closes their browser but does not log out, it will not update their record in the database until another person (logged in or guest) accesses the page in which the above code is being run on. If you have a cronjob running it then there would be no problem. (this also addresses your comment about the end session time getting executed, my site doesn't require that so happy you brought up the point, therefore a cronjob for you would be a must I think).
As for redirecting them to the logout page, I'm not sure how that would work in this case seeing as their page is not being refreshed. That would probably need to be ajax or something.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文