2

I have a session like this $_SESSION['login'] and when it is equal with 1, it means the use is logged into my website:

if ( $_SESSION['login'] == 1 ) {
    // You are logged
} else {
    // login/register
}

Also I have another session which contains user's id. Something like this:

echo $_SESSION["Id"]; 
/* It is containing the user's id (an integer number).
   Something like: 234124
*/

Now I want to unset $_SESSION['login'] for the user who has a specific id. For example I want to unset($_SESSION['login']) for $_SESSION["Id"] = 234124. How can I do that?


Edit: All I'm trying to do: When an user changes his password, I remove all his cookies from cookies table to sign him out from all his other devices. Also I want to remove his session.

12
  • session_start(); session_destroy(); the whole session you previously created will destroyed. Commented Jul 27, 2016 at 3:58
  • 2
    My question asks: how can I do that for an specific user?
    – stack
    Commented Jul 27, 2016 at 3:58
  • oh right @stack I misunderstood question.
    – user6322822
    Commented Jul 27, 2016 at 3:59
  • I don't know enough to be 100% sure about it but I'm pretty sure you would'nt be able to unset the session for a particular user you would have to redirect that user to a page which unset their session; as the session is stored locally rather than in a database; however I could be totally wrong.
    – user6322822
    Commented Jul 27, 2016 at 4:01
  • Can't see the point of having a session login and id, you can just unset the session id to log him out. Commented Jul 27, 2016 at 4:01

4 Answers 4

5

Updated Answer

You've provided helpful details in your comments:

When an user changes his password, I need to logout his account from all other his devices.

Your question is essentially how to implement single login/logout across devices if you're using sessions.

Here is a simple approach:

  1. User logs in, you set userID and lastSeen in session. lastSeen holds a timestamp. Save no info in session that the user can change.
  2. User logs into another device, you set userID and lastSeen in that session
  3. Sessions across devices are always in sync (except for lastSeen) because they only hold non-changing data (userID, userName)
  4. In your DB, have a logout table with columns userID requestTime
  5. If a user logs out, changes her password or does anything else that should require a re-login, call session_destroy() and add an entry in logout table
  6. When user tries to access restricted page, you check:
    • Does $_SESSION['userID'] exist (means user logged in at some point)
    • Is lastSeen within the last 30 minutes (otherwise, call session_destroy() and request another login)
    • Is there a logout request with the user's ID in logout and with requestTime > lastSeen (means since we last saw the user, she requested to be logged out from another device). If so, session_destroy() and require another login.

Original Answer

Sessions are handled in isolation. When a request arrives, the $_SESSION data for just that user is loaded in memory. So if userID 5 makes a request, you do not have access to the session data for user 7 (without some hacks).

If you want to unset the current user's session, whoever that user may be, you can do one of the following:

session_destroy(); //clears everything for the current user
unset($_SESSION['login']);// clears just this variable for the current user

If from one user's browsing session, you want to mess with another user: I don't see the use case. Sounds like it would have negative security implications, and it makes me question your greater architecture. It defeats the whole purpose of sessions: to provide each user an isolated, persistent storage locker on the server.

Anyway, to change a random user's session data from another user's browsing activity (again, why?), use a database to save and retrieve values instead. A table could be as simple as:

userID | sessionData | sessionExpires

You could store session data in JSON with json_encode and retrieve it with json_decode for any specific user, from any browsing session.

2
  • Thank you, upvote .. btw your activity is odd on the SO .. You had less than 2k rep just a few weeks ago, I mean you've earned lots of reputation recently . Why? Has you been hired in SO ;-)? What is your new motivation for being active in here?
    – stack
    Commented Jul 27, 2016 at 4:27
  • 1
    @stack I wasn't hired. Are you hiring? My motivation is learning. I've found that having to solve problems quickly and having to explain the solutions clearly forces me to learn a lot, and to practice efficient coding. Commented Jul 27, 2016 at 4:34
2

it will remove all session variables and destroy the all session you created.

<?php
//start session
session_start();
// remove all session variables
session_unset(); 
// destroy the session 
session_destroy(); 
?>
3
  • But the OP wants to target a specific user's session, by user ID. Not just the current user Commented Jul 27, 2016 at 4:15
  • then simply add the if condition which specifies the session variable. Commented Jul 27, 2016 at 4:17
  • i dont even understand why the session created like that? Commented Jul 27, 2016 at 4:18
2
if ( $_SESSION['login'] == 1 ) {
 if ( $_SESSION["Id"] == 234124 ) {
       session_destroy();   
       session_unset(); 
 }
} else {
  // login/register
}
0
1

use session_destroy();

It destroys all of the data associated with the current session. So only intended user's(who clicked logout or in your case who changed password) session data will be removed.

Check this to know more.

1
  • But the OP wants to target a specific user's session, by user ID. Not just the current user Commented Jul 27, 2016 at 4:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.