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

rogical’s picture

mysql> show create table users\G
*************************** 1. row ***************************
       Table: users
Create Table: CREATE TABLE `users` (
  `uid` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Primary Key: Unique user ID.',
  `name` varchar(60) NOT NULL DEFAULT '' COMMENT 'Unique user name.',
  `pass` varchar(128) NOT NULL DEFAULT '' COMMENT 'User�s password (hashed).',
  `mail` varchar(254) DEFAULT '' COMMENT 'User�s e-mail address.',
  `theme` varchar(255) NOT NULL DEFAULT '' COMMENT 'User�s default theme.',
  `signature` varchar(255) NOT NULL DEFAULT '' COMMENT 'User�s signature.',
  `signature_format` varchar(255) DEFAULT NULL COMMENT 'The filter_format.format of the signature.',
  `created` int(11) NOT NULL DEFAULT '0' COMMENT 'Timestamp for when user was created.',
  `access` int(11) NOT NULL DEFAULT '0' COMMENT 'Timestamp for previous time user accessed the site.',
  `login` int(11) NOT NULL DEFAULT '0' COMMENT 'Timestamp for user�s last login.',
  `status` tinyint(4) NOT NULL DEFAULT '0' COMMENT 'Whether the user is active(1) or blocked(0).',
  `timezone` varchar(32) DEFAULT NULL COMMENT 'User�s time zone.',
  `language` varchar(12) NOT NULL DEFAULT '' COMMENT 'User�s default language.',
  `picture` int(11) NOT NULL DEFAULT '0' COMMENT 'Foreign key: file_managed.fid of user�s picture.',
  `init` varchar(254) DEFAULT '' COMMENT 'E-mail address used for initial account creation.',
  `data` longblob COMMENT 'A serialized array of name value pairs that are related to the user. Any form values posted during user edit are stored and are loaded into the $user object during user_load(). Use of this field is discouraged and it will likely disappear in a future...',
  PRIMARY KEY (`uid`),
  UNIQUE KEY `name` (`name`),
  KEY `access` (`access`),
  KEY `created` (`created`),
  KEY `mail` (`mail`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Stores user data.'
1 row in set (0.00 sec)
bocaj’s picture

Status: Active » Closed (works as designed)

Hi 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.

rogical’s picture

Got 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.

bocaj’s picture

The 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.

rogical’s picture

Your're right, thanks for the explanation.

windmaomao’s picture

thanks 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."