Skip to main content
Known Participant
October 24, 2009
Question

Update table when user logs out?

  • October 24, 2009
  • 3 replies
  • 3267 views

I have a table called "users" with a field called logged_in that is updated to a "Y" when a user logs in.  That is working fine.  I need to update that a "N" when a user logs out, but I am having trouble.  I guess because it's not a submit button so I can't use the update wizard.  I am not that good at code, but have tried a few things and can't seem to get it twork.  Can anyone give me a simple code to insert to make this work?  OR even better a way that just logs out the user after 60 minutes and updates that field to "N".  Either way I could work with.  Thanks!

This topic has been closed for replies.

3 replies

David_Powers
Inspiring
October 30, 2009

Marking this thread as assumed answered.

Participating Frequently
October 26, 2009

Why do you need to update the database? Have you considered that many users will simply close their browsers or navigate to another site and not log out? So the user's session will timeout but the flag in the database will stay list them as logged in. Just make sure to account for these inconsistencies.

DwFAQ
Participating Frequently
October 26, 2009

Heya bregent,

If you've read my query you would have discovered that the script updates the database for the users logged in or logged out status regardless of whether the user actually logs out or not. If no activity from the user is made within 60 minutes then their status is updated to logged out when anyone visits the page the script is on which accounts for the inconsistencies you have mentioned.

Best,

Participating Frequently
October 26, 2009

Ok good, I had read the OP's message to mean log out after 60 minutes of inactivity.

DwFAQ
Participating Frequently
October 24, 2009

Hello?!?

Kind of hard to offer a solution when your scripting language is unknown!

PHP solution is provided below. Add a last_login_date field with datetime to your DB table and user_id that updates user in query based on Session variable of logged in user.

// Update Online Now Status

mysql_select_db($database_XXXXXX, $XXXXXX);

$logged_in_user = "-1";

if (isset($_SESSION['login_id'])) {

$logged_in_user = $_SESSION['login_id'];

$query_online_now = "UPDATE users

SET logged_in = 'Y',

last_login_date = NOW()

WHERE user_id = $logged_in_user";

$online_now = mysql_query($query_online_now, $XXXXXX) or die(mysql_error());

}

$query_not_online_now = "UPDATE users

SET logged_in = 'N'

WHERE last_login_date < NOW() - INTERVAL 60 MINUTE";

$not_online_now = mysql_query($query_not_online_now, $XXXXXX) or die(mysql_error());

// Update Online Now Status

Rick4209Author
Known Participant
October 25, 2009

Sorry. php

DwFAQ
Participating Frequently
October 25, 2009

Lucky for you a php solution has been provided.