When a user successfully authenticates on my site, i store their user id in session: $_SESSION['user_id']
I then use this throughout the site to check if the user can perform certain actions. e.g.
if(isset($_SESSION['user_id'])){
//User is logged in, allow the following.
...
}
...
if ( $_SESSION['user_id'] == $comment_user ) {
//User owns the comment, go ahead and delete it.
...
}
However, if i discover that a signed in user has malicious purposes, how can i kill their login session so that they cannot perform these secure actions?
To block the user, on the db I can invalidate their login details, or add them to a blocked list that is checked upon authentication so that they can no longer authenticate. However, this would only have effect when they next attempt to log in. As long as the current session remains active and their user id is stored in session, they are considered authenticated..
- Is there a way to unset a specific session, forcing a logout? How?
- If not, what is the best way to make sure blocked users cannot continue to access secure areas on the site? e.g. My only idea is rather than just checking
if(isset($_SESSION['user_id']))
, an additional check can be added to make sure the user_id hasn't been added to a "blocked users" list on the db. I just don't like that another db request is made to check if the user has been to a blocked list each time they perform some action. Especially because blocking a user would be a rare occurrence. Is there a way to check if the user has been blocked without going to the db?
Thanks!
Edit
Most answers so far address how to unset/destroy a session, or how to block a user from their next login attempt. I guess the only question remaining then is how to check whether a user has been blocked while they are currently logged in. Is there is a way to do this without going to the DB to check a "blocked users" list each time a user performs an action. This relates to my main issue, which in bold italics above. If the user is blocked then i can immediately destroy the session (forcing a logout) and they will also be prevented from authenticating again.