0

I have a site built with the PHP framework Yii and a cache table for search queries that are unique per user. I want to clear this cache as soon as the user has logged out, or their session have expired. I have an entry for the creation date and time of each search cache item.

If the user clicks logout, this is easy. I just clear their cache. But my problem is, how do I know if the session have expired if they don't click the logout button? I'm using the standard Yii user model, with sessions not stored in db but in files (I'm guessing PHP standard sessions is taking care of this).

How can I know for a given userId that have cached items that their session have expired ?

Niklas9
  • 8,816
  • 8
  • 37
  • 60
  • When user logs out, set the session entry as "logged_out" (new column) and then write a cron script that goes through session entries (marked "logged_out") and clear them? – Latheesan Oct 28 '13 at 10:16
  • Refer to this link for complete information on how you can do that: https://stackoverflow.com/questions/520237/how-do-i-expire-a-php-session-after-30-minutes – Mahesh Budeti Oct 28 '13 at 10:15
  • check the complete post they explain in either way. – Suresh Kamrushi Oct 28 '13 at 10:17
  • @mahesh this is not answering my question, I need to know in the Yii specific case, how to find out if a session have expired for a given userId, as said in my post – Niklas9 Oct 28 '13 at 11:06
  • You can only find your session has expired on the next request from the user, or, use a cron job to go through each login as has been suggested. – crafter Oct 23 '14 at 06:37

1 Answers1

0

When user logs in, set the session on the global scope like this:

Yii::app()->session['isLoggedIn'] = true;

Through out your controller, you can check if user is logged in or not like this:

// Check If User Is Logged In
if (isset(Yii::app()->session['isLoggedIn']) && Yii::app()->session['isLoggedIn'])
{
    // welcome user
} else {
    // welcome guest
}

To log user out, clear session data like so:

Yii::app()->session['isLoggedIn'] = false;
// or
unset(Yii::app()->session['isLoggedIn']);

More info on session management here: http://www.larryullman.com/2011/05/03/using-sessions-with-the-yii-framework/

Latheesan
  • 23,247
  • 32
  • 107
  • 201
  • Thanks, is this some way to do this in the CLI ? As I want to clear it when user has left the site completely, even without pressing the logout button – Niklas9 Oct 28 '13 at 12:23
  • If you are using session, then user is logged in whilst his browser window is open. The sec s/he closes the browser window, the session is destroyed, hence logging out the user. – Latheesan Oct 28 '13 at 15:02