开发者

PHP, how do I calculate total online time of a user?

What is the best way to calculate the total time spent by a registered user on the site? ...under these co开发者_运维问答nditions 1) User logs out normally. 2) User can simply close browser. 3) User can auto-login next time he comes back.

I think the best way to do this would be to find the time spent by the user on each page and keep adding them to his total time instead of checking for the whole site. But I don't know how to implement that....please help


You can't find the exact time he leaves the system, unless he logs out. Even then, he might be browsing the site while logged out.

The approximate way to do this would be to set the start time in the session and keep incrementing the time everytime he visits a page.

So the first time the user comes to your site at time T, you will

  • Create a session and put the start time there
  • Add the total time as 0

For all subsequent requests you would

  • Check the start time and compare that with the time now and get the difference
  • Add that time to the total time

This method will not give you the time the user spent on the last page. But it will give you something to work with.


You can do this with JavaScript and a separate PHP script. The javascript code reacts to events that mean that an user is active (such as mouse/keyboard/resize events) and invokes the php script.

The php script compares the time when it last received a request to the current time and checks if the difference is over a certain threshold (i suggest something like 10-30 minutes to prevent single-click sessions from adding up) nothing happens.

If the threshold is not reached then the difference between the two timestamps is added to the total sum in the database.

Afterwards (in both cases) the last request time is set to the current time and the script ends.

If you also want to know when the user closes your website pages you can subscribe to unload events and/or implement an heartbeat script that calls a PHP script every X seconds.


you have 2 approaches, either to create a log table in your DB to track each user (by ID) logins and logouts and then calculate the time difference between the two in each record for the specific user and then sum all of that. OR you go more complex and make 4 columns in your DB->usertable (logintime 'timestamp' - logouttime 'timestamp' - lastactive 'timestamp' - onlinetime 'int') and update each column as their names say by code according to user activities. then alter the Session.php script in the System/libraries directory at line 105 exactly after if ( ! $this->sess_read()) before the system creates a new session and write a code to check if the 'logouttime' is not the same as 'lastactive' time (to avoid session timeout expiry misunderstood in the next code) if both fields not the same, update your DB to make 'logouttime' equals 'lastactive' then at line 107 exactly after: $this->sess_update(); write a code to check if the 'logouttime' equals the 'lastactive' (and you will make that happen earlier in your logout.php script) write a code to calculate the online time by the difference between the 'logintime' and the now time 'time()' and add the result to the 'onlinetime' field. but if the 'logoutime' is not the same as 'lastactive' (that meanse the user is online and making activities in your site because you are tracking him and updating the 'lastactive' field frequently) then write a code to calculate the online time by the difference between the 'lastactive' and now time 'time()' and add the result to the 'onlinetime' field. that way you have the exactly online time logged forever in the 'onlinetime' field! I hope you got me right because the examples will be a lot long of scripts (although I don't mind to share upon request). good luck.


Use the Session ID to keep track of individual sessions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜