4

嗨,我不确定这是怎么可能的,但是是否有一个简单的代码会从用户访问我的网站的那一刻开始计数,我可以使用标题标签来存储带有一些 php 代码的会话来说明何时访问页面设置一个时间并将时间存储在我的mysql表中并计算直到用户离开站点/会话结束的那一刻,并根据计算的时间量计算结束时间?

我想在我的数据库中创建一个名为会话的表,所以我知道我的网站上有多少人,我知道有谷歌分析,但我想实时管理这个,所以很感激任何帮助或点正确的方向谢谢

// GET IP ADDRESS
 $sql = "INSERT INTO ptb_sessions (session_id, user_ip, session_start, session_end) VALUES (NULL, '" . $_SERVER['REMOTE_ADDR'] . "', now() '....');";
                mysql_query($sql, $connection);
4

3 回答 3

4

根据我的经验,跟踪用户的最佳方法是结合 ajax 和 php。您应该在特定的时间间隔(比如说 60 秒)内从 javascript 发送通知,您可以在 php 端更新用户当前登录您网站的时间的值。

示例代码:

索引.html

<script type="application/x-javascript">
// js code in index.html

window.setInterval( function(){
    var xmlhttp;
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.open("GET","background.php?ping=1",true);
    xmlhttp.send();
}, 60000 );
</script>

背景.php

<?php
session_start();
// ... your database initialization here, so $connection will be valid variable ...

// background.php
// ur initialisation
$session_id = session_id();
$sessionEnd = date('Y-m-d h:i:s');
$sql = "INSERT INTO ptb_sessions (session_id, user_ip, session_start, session_end) VALUES ('$session_id', '" . $_SERVER['REMOTE_ADDR'] . "', now(), '$sessionEnd') ON DUPLICATE KEY UPDATE session_end = '$sessionEnd'";
mysql_query($sql, $connection);

?>

注意:您需要在后台脚本中使用session_start() ;重复构造的 sql 将自动更新您的值 - 因此无需编写存在检查。

于 2013-05-02T06:33:10.443 回答
1

没有。PHP 是服务器端的,所以一旦网络服务器将页面发送出去,就是这样。你也许可以在 javascript 中做到这一点

可以做的一种可能的变体是将最后一个页面视图视为“会话结束” - 在设定的时间段(称为 20-30 分钟)后有一个会话超时,并且无论最后一页加载的时间是什么时候会议结束。您可以使用数据库来跟踪上次加载的页面时间。

你可以很容易地得到Session_Id, Session_start,IP但不是Session_end

于 2013-05-02T06:30:36.060 回答
0

Even though PHP is a server side script, you might think about another approach and use php to analyze your webserver log to estimate the time a customer has visited your site. Keep in mind, that this cannot be perfectly accurate (as any JS solution e.g. Google Analytics would be, too). It is just an approximation based on path analysis.

176.28.xx.xx - - [28/Apr/2013:19:26:04 +0200] "GET /page1 HTTP/1.1" 200 5552 "-" "-"
176.28.xx.xx - - [28/Apr/2013:19:26:05 +0200] "GET /page2 HTTP/1.1" 200 5552 "-" "-"
176.28.xx.xx - - [28/Apr/2013:19:26:06 +0200] "GET /page3 HTTP/1.1" 200 5552 "-" "-"
176.28.xx.xx - - [28/Apr/2013:19:26:07 +0200] "GET /page4 HTTP/1.1" 200 5552 "-" "-"
176.28.xx.xx - - [28/Apr/2013:19:31:04 +0200] "GET /page4 HTTP/1.1" 200 5552 "-" "-"
176.28.xx.xx - - [28/Apr/2013:19:31:05 +0200] "GET /page1 HTTP/1.1" 200 5552 "-" "-"

In this case you would find out, that there is one customer (based on the assumption, that a customer is uniquely identified by his IP address) that has two intervals of (based on the assumption that we put up a threshold of 1 minute, where a new visit of the same customer begins)

  • 3 seconds from 28/Apr/2013 19:26:04 - 19:26:07
  • 1 second from 28/Apr/2013 19:31:04 - 19:31:05

A tool already doing that is AWStats

Further readings can be found if you are looking for

  • data mining
  • path analysis

Bottom Line: But this is just an approach to answer your question. I would use tools like Piwik, Google Analytics or similar to track those. They are advanced and doing all the stuff for you, especially storing lots of plotting points, that are needed. Consider that such tracking tools increase the request rates to your webserver dramatically and since php needs to setup a database connection on each call, that tool you are seeking to build, will be quite expensive in server power.

于 2013-05-02T06:55:22.933 回答