How to know how much time a user spent in a page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
alxkn
Forum Newbie
Posts: 14
Joined: Sat May 20, 2006 5:14 pm

How to know how much time a user spent in a page

Post 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.
Chonk
Forum Newbie
Posts: 24
Joined: Fri May 28, 2004 3:58 am

Post 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 ? ) .
alxkn
Forum Newbie
Posts: 14
Joined: Sat May 20, 2006 5:14 pm

Post by alxkn »

Thanks. How can I note the time in session variable for each member.?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
alxkn
Forum Newbie
Posts: 14
Joined: Sat May 20, 2006 5:14 pm

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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'];
?>
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

Could I ask what this is for? A game? If so there could be better solutions for this.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post 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 :P

Cheers :)
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

hockey you look just like my roommate.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

Old picture...bad picture...I look like I'm snorkling in a bathub :P

Edit: May not be suitable for all viewers...viewer discretion is advised :P

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... :)
alxkn
Forum Newbie
Posts: 14
Joined: Sat May 20, 2006 5:14 pm

Post 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.
alxkn
Forum Newbie
Posts: 14
Joined: Sat May 20, 2006 5:14 pm

Post 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 :P

Cheers :)
Sorry, I took a look to FAQ, there was not this kind of question.
Post Reply