Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
there're already 2 time fields in users table: access and login.
access means last visit, login means last login.
mysql> desc users;
+------------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------+------------------+------+-----+---------+-------+
| uid | int(10) unsigned | NO | PRI | 0 | |
| name | varchar(60) | NO | UNI | | |
| pass | varchar(128) | NO | | | |
| mail | varchar(254) | YES | MUL | | |
| theme | varchar(255) | NO | | | |
| signature | varchar(255) | NO | | | |
| signature_format | varchar(255) | YES | | NULL | |
| created | int(11) | NO | MUL | 0 | |
| access | int(11) | NO | MUL | 0 | |
| login | int(11) | NO | | 0 | |
| status | tinyint(4) | NO | | 0 | |
| timezone | varchar(32) | YES | | NULL | |
| language | varchar(12) | NO | | | |
| picture | int(11) | NO | | 0 | |
| init | varchar(254) | YES | | | |
| data | longblob | YES | | NULL | |
+------------------+------------------+------+-----+---------+-------+
16 rows in set (0.05 sec)
Comments
Comment #1
rogical CreditAttribution: rogical commentedComment #2
bocaj CreditAttribution: bocaj commentedHi rogical!
You are correct about those two fields already being in the users table. However, those two fields don't record data as expected (or at least not as I expected). I had a project that I needed to show the users' actual last login time in a block with other information. I tried both fields over and over thinking I was doing something wrong.
I can't find the page that clued me in to what was going on, but basically the 'access' field is the last time that the user accessed any page. So if a user logs in at 2:00pm and then accesses 5 pages over the next 5 minutes, the 'access' field is going to have the value of 2:05pm (of course, this is in unix timestamp format). Any time a user "accesses" the site, then this field is updated.
The 'login' field does show the user's last login, but according to Drupal the "last login" was just now. This field always holds the user's most recent login. So, in my example above, the 'login' field will show as 12/13/2011 at 2:00pm (in unix timestamp format), which is when I just logged in, this session. I needed to be able to do something like, "Hello user, you last logged in at 12/11/2011 at 11:12am". This functionality is not available in Drupal by default, that's why I created this module.
Hopefully this makes sense!
If I can find that page again that I found when building this functionality I will post it on the project page.
Comment #3
rogical CreditAttribution: rogical commentedGot it, thanks.
But I think it would be more fine to create another table to record the timestamp than add field to users table -- which is created by core.
It's better if we modify the default stuffs as less as possible.
Comment #4
bocaj CreditAttribution: bocaj commentedThe reason it is in the users table is so that it is available through the global $user variable and also through user_load() function. I could create a function that would pull the information from a separate database table, but that seems like more work than is necessary.
I think the current implementation is the cleanest way to accomplish what the module is intended to do. I think that having the last_login information available with the global $user variable and in user_load() is a bonus.
Comment #5
rogical CreditAttribution: rogical commentedYour're right, thanks for the explanation.
Comment #6
windmaomao CreditAttribution: windmaomao commentedthanks for the explanation. Some of the text probably should be on the front page. ex.
"just logged in, this session. I needed to be able to do something like, "Hello user, you last logged in at 12/11/2011 at 11:12am". This functionality is not available in Drupal by default, that's why I created this module."