Recording a user's last login date and then retrieving it

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
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Recording a user's last login date and then retrieving it

Post by Luke »

I am building a file manager and I would like any files that have been uploaded since a user's last login to show up in a different color or something, but I can't FOR THE LIFE OF ME figure out how that can be accomplished... I've been cracking away at it all morning... anybody? Thanks in advance!
User avatar
TheMoose
Forum Contributor
Posts: 351
Joined: Tue May 23, 2006 10:42 am

Post by TheMoose »

Whenever a user logs in, store that login time in a database. Then every time a file is uploaded, record that upload time in a database. If the user's login time is less than the file upload time, that means the file was uploaded after their latest login, so change the color. If the login time is greater than the upload time, that file was uploaded before their last login, so do the normal color. What you could also do is have it store their LOGOUT time instead, that way it will truely be the files since they last visited the page.

Pseudo:

Code: Select all

user_login_time = store_login_time();
if(user_login_time < file_upload_time)
  file_is_new;
else
  file_is_old;
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

TheMoose wrote:What you could also do is have it store their LOGOUT time instead, that way it will truely be the files since they last visited the page.
This is what I am trying to do. I don't need to save the time files were uploaded into a database, because the server records that information anyway. I can retrieve that with filemtime(). The reason I am having problems is because what if the user doesn't actually log out? What if they just close the window? How would I then insert that information in the database? Maybe i could use cookies or something? I've never actually worked directly with cookies. (just with session functions). any advice?
User avatar
TheMoose
Forum Contributor
Posts: 351
Joined: Tue May 23, 2006 10:42 am

Post by TheMoose »

On each page load, you can store the time the page loaded in the database (their last activity). I believe the default timeout for sessions is 20 minutes, so you can take the last activity time, and if the current time is 20 minutes after that time, then you can assume that the person left the page since there is no more activity.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

You can have the your user database have a field for logged in, ever page would update the field to 1, and then make another field for update login time. And then have a cron-job that goes and check if current time is greater than 15 min set 1 to 0 and then store the current time as their last login time. Then just make it so if checks the time the user last logins and when the files were uploaded and if its after the last login time, have PHP print a color code or whatever you want.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

One of the techniques I use is to track user activity. I keep track of their IP, their login status, their admin level, their user status, their last login time, their last page and last page load time. Just those pieces of information alone can be used for any number of things that you might want to show (ie, Who's logged in, what has happened since your last loging, where the users are geographically [though IP geo locating is unreliable], etc).
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post by bdlang »

Everah wrote:One of the techniques I use is to track user activity. I keep track of their IP, their login status, their admin level, their user status, their last login time, their last page and last page load time. Just those pieces of information alone can be used for any number of things that you might want to show (ie, Who's logged in, what has happened since your last loging, where the users are geographically [though IP geo locating is unreliable], etc).
I'm curious, do you use a seperate table for this or within the 'users' table? I've always used a seperate table myself.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

bdlang wrote:
Everah wrote:One of the techniques I use is to track user activity. I keep track of their IP, their login status, their admin level, their user status, their last login time, their last page and last page load time. Just those pieces of information alone can be used for any number of things that you might want to show (ie, Who's logged in, what has happened since your last loging, where the users are geographically [though IP geo locating is unreliable], etc).
I'm curious, do you use a seperate table for this or within the 'users' table? I've always used a seperate table myself.
I tend to keep logging information in it's own table..
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I usually keep everything related to the user in the user table. Though you can probably do this anyway you want. If you want to keep a running log then I would suggest you place this information into a separate table. If you are only interested in what is happening now or what has happened since the last time, keeping that data in the user table should work without a problem.
printf
Forum Contributor
Posts: 173
Joined: Wed Jan 12, 2005 5:24 pm

Post by printf »

bdlang wrote:I'm curious, do you use a seperate table for this or within the 'users' table? I've always used a seperate table myself.
If your not really using the data, other than to show the user their activity, I would create another table this way you only access the data in a JOIN, I would also just use (2) columns, the user_id as the ref key, and a text column, then serialize the activity data. But like I said I would only do that if it was for display purposes only. If it was for more, then I would keep certain values in their own column so I could select, update and delete certain data that I want to manage!

pif!
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post by bdlang »

Everah, Jcart, printf>

Thanks for your responses; I actually tend to do both. I always have an `activity_date` update-always TIMESTAMP column in the `user` table (along with the generic, update-once-only `register_date` column). Then depending on the application I have a `user_activity` table that stores the UserID and the other current info as previously mentioned. That way you can prune outdated `user_activity` records as needed and still store the last activity date for every user.

It's like anything, everybody has a unique way of doing things, I'm always curious of other's methods so I can learn a better way. I like printf's logic of storing a serialized array of user data, for example. ;)
Post Reply