Jump to content

Track User Login & Spent Time Statistics


coderzm
Go to solution Solved by iarp,

Recommended Posts

Hi all,

 

I already have a simple login script.

 

However, I am trying to create a new feature that allows me to check the number of logins for a user, and the ability to filter by time periods and total time spent per session.

 

How do I make sure of the SESSION class to do that? Is it possible to do this via pure PHP?

Any advice is appreciated. Thanks.

Link to comment
Share on other sites

  • Solution

Add a new column to the users table, call it whatever you want maybe TotalLogins, and within whatever script you use to login the user add a mysql statement

UPDATE USERS SET TotalLogins = TotalLogins + 1 WHERE id = <the user id>

That way you would know just how many times they have logged in.

 

For tracking session length you'll most likely need another table. It'll need to track Users ID, Session ID, Login datetime, Last Activity datetime.

 

When the user logs in, INSERT into this new table using the users id from login, session_id(), now(), now(). Then on every page you need an update statement that says something like

UPDATE UsersSessionTracker SET last_activity = NOW() WHERE users_id = <the users id> AND session_id = <session_id() from php>

Of course you'll need check to ensure the row exists and what to do if it doesn't...etc.

 

This isn't foolproof, it bases the session length on the last page load that they did from your website. If they site on that page for 15 minutes and then leave, you wouldn't know that they sat there for 15 minutes on a single page. For that type of tracking you would need Javascript that sends calls back to the server every x minutes to keep track..etc.

Link to comment
Share on other sites

Hi iarp,

 

Thanks for your help.

 

I implemented TotalLogins  and it works pretty well. 

 

For the second part, I am not very clear about the session_id variable/method. How do I get the session_id and assign it to a variable? 

 

To clarify - The session_id is assigned to a variable at the login page or at the user registration page? 

 

 

Add a new column to the users table, call it whatever you want maybe TotalLogins, and within whatever script you use to login the user add a mysql statement

UPDATE USERS SET TotalLogins = TotalLogins + 1 WHERE id = <the user id>

That way you would know just how many times they have logged in.

 

For tracking session length you'll most likely need another table. It'll need to track Users ID, Session ID, Login datetime, Last Activity datetime.

 

When the user logs in, INSERT into this new table using the users id from login, session_id(), now(), now(). Then on every page you need an update statement that says something like

UPDATE UsersSessionTracker SET last_activity = NOW() WHERE users_id = <the users id> AND session_id = <session_id() from php>

Of course you'll need check to ensure the row exists and what to do if it doesn't...etc.

 

This isn't foolproof, it bases the session length on the last page load that they did from your website. If they site on that page for 15 minutes and then leave, you wouldn't know that they sat there for 15 minutes on a single page. For that type of tracking you would need Javascript that sends calls back to the server every x minutes to keep track..etc.

Link to comment
Share on other sites

Hi iarp,

 

Thanks for your clarification. 

Reference to your earlier example, where 

UPDATE UsersSessionTracker SET last_activity = NOW() WHERE users_id = <the users id> AND session_id = <session_id() from php>

If session_id is different every time a user logs in, how will session_id match current session_id()?

 

EDIT: Okay, I realized I missed a step. I am supposed to insert the session_id() where user logs in, and then update last_activity when a user hits a new page where session_id = session_id()

Edited by coderzm
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.