php - 跟踪用户在页面上停留的时间?

标签 php javascript jquery

如何跟踪用户在请求另一个页面或离开网站之前在页面上停留的时间?

基本上,我想做一个检查,如果用户在页面上停留 20 分钟或更长时间,然后做一些事情。

我相信这需要 php 和 javascript,但我不确定如何完成它。

也许在php中使用这个$_SERVER来获取执行时间,然后在用户点击其他地方时获取时间戳并简单地比较两者?

最佳答案

您可以使用简单的 javascript 完成所有这些。
对于单个页面:

window.setTimeout(function(){
// Do stuff after 20 minutes of loading the page
// Then using jQuery you can call a PHP script to do stuff like so:
$.ajax({
     url: '/myScript.php',
     method: 'POST',
     success: function(result){
        //The request was successful and the output is stored in a variable: result
     },
     complete: function(){
        //Do stuff when the request is completed (Ignores if success or not)
     },
     beforeSend: function(){
        //Do something before the request is sent
     }
});
}, 20 * 60 * 1000); //This is in milliseconds that's why I use the equation 


对于多个页面:
我建议您在用户点击页面时设置一个 cookie,并在每个页面上检查 cookie 是否存在。如果存在,则每隔 x 秒运行一次查询,以查看自创建 cookie 以来是否已过去 20 分钟。


要获得完整的 Ajax 文档,请访问:http://api.jquery.com/jQuery.ajax/

关于php - 跟踪用户在页面上停留的时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15558475/

相关文章:

javascript - 在选择菜单 JQuery 中选择一个选项时添加 <td>

javascript - 如何使用 javascript 或 jquery 更改 html 元素的 css 属性

javascript - webpack-dev-server 热重载不起作用

php - 在PHP中转换为 bool 值

PHP + GD : imagetruecolortopalette not keeping transparency

php - PHP 异常会停止执行吗?

javascript - 正确改变 react 组件状态

javascript - 连接 CSS/JS 资源背后的逻辑

javascript - 生成随机值,保留返回值的历史记录

php - 如何在 php 中使用 fpdf 打破 mutlicell 中的行?