# Thread: record how many minutes is user on the page

1. ## record how many minutes is user on the page

I have a poll on the page and I want to calculate how many minutes user needed to solve it.

Has php any function which shows how many minutes a user has been in a page? Or is any other solution to make this?

Thank you

2. You would need to create a timestamp when they enter the page, pass it in the form, and compare it to the current time.

3. Yep, a timestamp on load and timestamp on submit should do it.

4. Try this:

PHP Code:
``` mysqy_query = "SELECT  TIMESTAMPDIFF(MINUTE, first_visited, NOW()) AS MinsSinceLastVisit FROM yourtablename id= 'yourid' ");if(mysql_num_rows(\$sqlpage) > 0){while(\$row1 = \$op->select(\$sqlpage)){\$minutes_dif = \$row1['MinsSinceLastVisit'];       \$Ymin = 60 * 24 * 365;       \$Mmin = 60 * 24 * 30;       \$Wmin = 60 * 24 * 7;       \$Dmin = 60 * 24;       \$Hmin = 60;       \$Y = (int)(\$minutes_dif / \$Ymin);       \$minutes_dif = \$minutes_dif &#37; \$Ymin;       \$MON = (int)(\$minutes_dif / \$Mmin);       \$minutes_dif = \$minutes_dif % \$Mmin;       \$W = (int)(\$minutes_dif / \$Wmin);       \$minutes_dif = \$minutes_dif % \$Wmin;       \$D = (int)(\$minutes_dif / \$Dmin);       \$minutes_dif = \$minutes_dif % \$Dmin;       \$H = (int)(\$minutes_dif / \$Hmin);       \$minutes_dif = \$minutes_dif % \$Hmin;        if(\$Y > 0 ){              echo "\$Y year(s) ";              }else if(\$MON > 0){              echo "\$MON month(s) ";              }              else if(\$W > 0){              echo "\$W week(s) ";              }              else if(\$D > 0){              echo "\$D day(s) ";              }              else if(\$H > 0){              echo "\$H hour(s) ";              }else if(\$minutes_dif > 0) echo "\$minutes_dif minute(s) ";else if(\$minutes_diff <= 0) echo "A minute ";}}  ```

5. A bit over-the-top for a simple poll Deepson2

@hash - sending it in the form might not be the best solution, especially if the user is aware of them being timed and want to alter the initial timestamp (easy in a form) to make it appear that they did it faster (it happens, believe it or not!)

A session value would be better, as the user cannot access it.

6. Well, you might want to expose that intentionally in order to bust users cheating

7. Orrrr you could not let them cheat in the first place?

8. If you wanted to expose them cheating, then not letting them try to cheat would fail :P

The op is asking "how can I get php to tell me the time spent on a page", not how can I protect myself from potential poll cheaters

But yes, session would indeed be better than post.

9. tnx!

10. Regardless of whether you embed the time into the form in some way, or whether you put it in a session, consider how your system will behave if a user was to refresh the page, or open another tab, or even open another window in a different browser.

11. ... or have two laptops

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•