Search code examples
phpauthenticationsessionlogout

PHP, login and logout time in temporary table?


I just read an article somewhere. They are using session_id store the login and logout time for every successful logged in user in a temporary table. Which means, the table would be deleted once the session destroyed. So why are they creating the tables??

Is there any use of the temporary tables? And why must use session_id??

What are the others uses of session_id in login and logout temporary table?? (I just cant figured out the connection and use of the session_id, login and logout time in temporary tables.)


Solution

  • Instead of using a second table why not use the users table to store this information?

    Users table:

    CREATE TABLE `Users` (
        `Id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
        `Username` VARCHAR(32) NOT NULL,
        `Password` CHAR(32) NOT NULL,
        `Login` TIMESTAMP DEFAULT 0,
        `Logout` TIMESTAMP DEFAULT 0,
        PRIMARY KEY (`Id`),
        UNIQUE KEY (`Username`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1
    

    Now when the user logs in store his id in a $_SESSION variable and update the login timestamp:

    UPDATE `Users` SET `Login` = NOW() WHERE `Id` = id_stored_in_session;
    

    and then when he logs out use the same command from above but on Logout