Discussion:
Using WP Transients for User Sessions
BenderisGreat
2013-10-16 13:58:28 UTC
Permalink
Basically I am using transients to setup a timeout after users login, so
active users names will have a little green label by them. After the time
period, that dot disappears. However I know on a busy site transients can
be troublesome, so I setup (I think, I have no idea if I did this properly)
a delete_transient on logout.

I am checking the database and I dont see anything being deleted from
wp_options, still has the same...


_transient_timeout_feed_mod_ac0b00fe65abe10e0c5b58...
1380374961

you get the idea. So I am hoping someone more familiar with this can check
out my code here. Now I should mention that the delete function APPEARS to
be working. On logout, the user session label is immediately changed (vs it
staying on for 30 mins or so without the function).

Does this all look like its functioning properly? Why am I not seeing the
hash delete from wp_options upon logout?

*add_action('wp', 'update_online_users_status');
function update_online_users_status(){

if(is_user_logged_in()){

if(($logged_in_users = get_transient('users_online')) === false)
$logged_in_users = array();

$current_user = wp_get_current_user();
$current_user = $current_user->ID;
$current_time = current_time('timestamp');

if(!isset($logged_in_users[$current_user]) ||
($logged_in_users[$current_user] < ($current_time - (15 * 60)))){
$logged_in_users[$current_user] = $current_time;
set_transient('users_online', $logged_in_users, 30 * 60);
}
}
}


function is_user_online( $user_id ) {
$logged_in_users = get_transient( 'users_online' );
return isset( $logged_in_users[$user_id] ) && (
$logged_in_users[$user_id] > ( current_time( 'timestamp' ) - ( 15 * 60 )
) );
}
*

and on logout, its calling this:

*function clear_transient_on_logout() {

delete_transient('users_online');
}
add_action('wp_logout', 'clear_transient_on_logout');*




--
View this message in context: http://wordpress-hackers.1065353.n5.nabble.com/Using-WP-Transients-for-User-Sessions-tp42547.html
Sent from the Wordpress Hackers mailing list archive at Nabble.com.
Otto
2013-10-16 14:06:54 UTC
Permalink
On Wed, Oct 16, 2013 at 8:58 AM, BenderisGreat
Post by BenderisGreat
I am checking the database and I dont see anything being deleted from
wp_options, still has the same...
_transient_timeout_feed_mod_ac0b00fe65abe10e0c5b58...
1380374961
...
Post by BenderisGreat
delete_transient('users_online');
Just checking here, but it seems like you're looking at a different
transient (the "feed_mod_whatever" one). That transient is for storing
the feeds being retrieved from other places.

Each transient entry has its own row for the transient's data and
another for the timeout. Since yours is "users_online", you should
look for "_transient_timeout_users_online" instead.

-Otto
J.D. Grimes
2013-10-16 14:11:38 UTC
Permalink
_transient_timeout_feed_mod_ac0b00fe65abe10e0c5b58…
1380374961
Yours would look have 'users_online' in it. The easiest way to check if it was deleted is to call get_transient(). If it returns false, then the delete must have worked (or it is expired).

Secondly, there is a problem with your logic here, I think. You are deleting the data for all logged in users, when a single user logs out. I don't think that is what you want to do. You will want to either use a different transient for each user, or just delete them from the array when they log out.

--J.D.
BenderisGreat
2013-10-16 16:22:34 UTC
Permalink
thats what I thought it was doing, but I wasnt sure because a var_dump looks
like each session is stored via the user_id

array(2) { [1]=> int(1381939181) [13]=> int(1381940097) }

how would I go about calling delete_transient() from an array like that. On
http://codex.wordpress.org/Function_Reference/delete_transient it says it
only accepts a single string.



--
View this message in context: http://wordpress-hackers.1065353.n5.nabble.com/Using-WP-Transients-for-User-Sessions-tp42547p42552.html
Sent from the Wordpress Hackers mailing list archive at Nabble.com.
J.D. Grimes
2013-10-16 16:30:20 UTC
Permalink
Post by BenderisGreat
thats what I thought it was doing, but I wasnt sure because a var_dump looks
like each session is stored via the user_id
array(2) { [1]=> int(1381939181) [13]=> int(1381940097) }
how would I go about calling delete_transient() from an array like that. On
http://codex.wordpress.org/Function_Reference/delete_transient it says it
only accepts a single string.
Don't call delete_transient(). Do something like this:

<?php

function clear_transient_on_logout() {

$logged_in_users = get_transient( 'users_online' );

$user_id = get_current_user_id();

// Remove this user.
unset( $logged_in_users[ $user_id ] );

set_transient( 'users_online', $logged_in_users );
}
add_action('wp_logout', 'clear_transient_on_logout');

?>

-J.D.

Loading...