Page 1 of 1
How to know how much time a user spent in a page
Posted: Tue Jun 27, 2006 12:46 pm
by alxkn
Hello,
I have a website and I need to write a php code that counts time spend by a member in each page of the website.
Could you please let me know how it can be accomplised or any referenses.
Thank you in advance.
Posted: Tue Jun 27, 2006 12:55 pm
by Chonk
Each time the user views a page note the time in the DB or session variable.
When the user then loads another page if the DB/session variable contains a time compare that to the current time and you have your time spent(technically the time since the last page refresh). Since its the time since the last refresh if it were over an hour i would ignore it (unless you have a lot of content).
Its then up to you how you want to record it (is it stats for each user or a general site stat ? ) .
Posted: Tue Jun 27, 2006 1:50 pm
by alxkn
Thanks. How can I note the time in session variable for each member.?
Posted: Tue Jun 27, 2006 2:01 pm
by RobertGonzalez
alxkn wrote:Thanks. How can I note the time in session variable for each member.?
Are you trying to do this as an admin so you can see the times? If so, using the idea posted after your first one will not give YOU the data, but it will make it avaiable to the users. For you to get the information, you will either need to sneak into your session data folder on your server or database the information and grab it at a later time.
Sessions, like cookies, are between the client and the server. The only difference is that cookies store on the client and sessions store on the server.
Posted: Tue Jun 27, 2006 5:31 pm
by alxkn
OK. Here is the problem. I have a website with multiple pages. I want the code that does not allow a registred user to go to another page after let say less than 2min. If a user tries to load the next page the code must give warning that you were in this page less than 2 min. If this must be done in sessions, then how exactly it must be written?
Thanks in advance.
Posted: Tue Jun 27, 2006 5:51 pm
by RobertGonzalez
Not sure how this will work, but it could be a start.
Code: Select all
<?php
session_start();
$_SESSION['this_page_time'] = time();
if (isset($_SESSION['past_page_time']))
{
// If the time check fails, send an error, otherwise let it ride
if ($_SESSION['this_page_time'] - $_SESSION['past_page_time'] <= 120)
{
// It hasn't been two minutes yet, error out
}
}
// At this point, the session time check has either failed and errored, OR
// Not failed and passed OR
// Was not checked because of a fresh page load.
// Either way, we are good at this point, so the past_page_time to now
$_SESSION['past_page_time'] = $_SESSION['this_page_time'];
?>
Posted: Tue Jun 27, 2006 8:36 pm
by daedalus__
Could I ask what this is for? A game? If so there could be better solutions for this.
Posted: Tue Jun 27, 2006 9:04 pm
by alex.barylski
Isn't this such a common question, in the sense that it's likely already been answered???
I'm not sure where it would be located...
Tutorials, code snippets maybe???
Hasn't this been done before and been posted in a FAQ???
Sorry if this sounds harsh, but there are some questions which seem to always be asked...at least once a week...
If it's not already coverd I suggest someone write something quick and show how it's done
Cheers

Posted: Tue Jun 27, 2006 9:13 pm
by daedalus__
hockey you look just like my roommate.
Posted: Tue Jun 27, 2006 9:27 pm
by alex.barylski
Old picture...bad picture...I look like I'm snorkling in a bathub
Edit: May not be suitable for all viewers...viewer discretion is advised
Seriously...I'm a pretty *not so nice* guy sometimes...so I may do or say something here which might offend some readers...use caution!!!
http://www.myspace.com/fukneh
Newer pictures...I don't really look the same...

Posted: Tue Jun 27, 2006 10:28 pm
by alxkn
Daedalus- wrote:Could I ask what this is for? A game? If so there could be better solutions for this.
Yes. This is a game. But I formulated the problem in a very simplified form.
Posted: Tue Jun 27, 2006 10:30 pm
by alxkn
Hockey wrote:Isn't this such a common question, in the sense that it's likely already been answered???
I'm not sure where it would be located...
Tutorials, code snippets maybe???
Hasn't this been done before and been posted in a FAQ???
Sorry if this sounds harsh, but there are some questions which seem to always be asked...at least once a week...
If it's not already coverd I suggest someone write something quick and show how it's done
Cheers

Sorry, I took a look to FAQ, there was not this kind of question.