views:

422

answers:

4

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

+2  A: 

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

Cris McLaughlin
A: 

"A TEMPORARY table is visible only to the current connection, and is dropped automatically when the connection is closed." (original)

So temporary tables definitely is a no-go. I'll vote for Cris' solution.

fabrik
A: 

i bet they're using the temporary table to store session information (using session_set_save_handler) and this is good: session information changes very fast and often, temporary tables are perfect for such solutions

and maybe you confused temporary with memory? memory tables aren't stored on your harddrive and data is lost on reboot, which is feasable for session information

knittl
A: 

session id are very userful to recognize the whether the users are logged in or not

just create a login table with username,session-id,recentaccesstime

when ever the user requests a new page chechk whether the sessions-id and username are matching or not

if thye were matching just update the recentaccesstime

if the recentaccesstime is > 60 minutes you can set the user logged out of our site and delete his entity from the login table

www.addressinformer.com

sainath