1

I try to destroy the Session from a other user after I changed his permissions or his ban-status...

I write the session key from every user in to the database and catch them if I visits there Profiles. If there are any changes I want that the User will get kicked out of the System instantly...

Here is my code:


    FUNCTION back_to_home() {
            mysqli_close($db);
            $session_id_to_destroy = $_SESSION['visit_user-session_id'];
            session_id($session_id_to_destroy);
            session_start();
            session_destroy();
            unset($_SESSION['visit_user-username']);
            unset($_SESSION['visit_user-e_mail']);
            unset($_SESSION['visit_user-register_date']);
            unset($_SESSION['visit_user-last_login_date']);
            unset($_SESSION['visit_user-register_ip']);
            unset($_SESSION['visit_user-last_login_ip']);
            unset($_SESSION['visit_user-steam_id']);
            unset($_SESSION['visit_user-permissions']);
            header('Location: ../../../');
            exit;
        }

I hoped I can fix or do that with PHP. I have no clue of JavaScript xD

So what I want to know is, Can I do that like that or is there another way to kick out another user from his session?

8
  • 3
    Uhm, you forgot to ask a question. Commented Jul 31, 2019 at 8:50
  • You can't really modify the session of someone else that easily. You can use webhooks to listen for change on permissions/bans, or just check on the next request (from that user).
    – Qirel
    Commented Jul 31, 2019 at 8:52
  • Why not put all session data in the database, and then delete whatever row you want to delete?
    – Nico Haase
    Commented Jul 31, 2019 at 8:53
  • So in my case I need a "retroactive check" in every new request I am right?
    – NvrKill
    Commented Jul 31, 2019 at 8:57
  • 2
    Possible duplicate of Unset a specefic session using session id
    – Kapsonfire
    Commented Jul 31, 2019 at 9:12

1 Answer 1

1

I try to destroy the Session from a other user

You can technically do this, yes, but the process is long winded and problematic.

How to delete an arbitary session.

A: Finding the session

You need to use the session_id value. This value is part of the file name (or in the case of database sessions the session identifier column value).

You need to know the file name precursor (usually sess_ but can be anything as set in the PHP source code). You also need to know the session storage location as set in your PHP.ini file.

Example:

A session with id 58ce93c623376b3ddfca3cfc3a01d57d3be85084363147464 is a file at:

/home/session_storage_folder/sess_58ce93c623376b3ddfca3fc3a01d57d3be85084363147464

But session file names are generated on the fly and are not (and should not be) connected to whose who on your membership database.

If you generate session id's manually then this becomes easier, but the security of sessions becomes greatly reduced and this should really, really not be done without very careful thought.

B: Finding the user

Now you need to find the User that you want to ban. The session file will contain the users id, somehow,

Session data is stored typically as:

(Example)

$_SESSION['alpha'] = "UiOvMfV9byatH4Wt1SPYUO3zgsj5";
$_SESSION['beta']  =  1324; 

alpha|s:28:"UiOvMfV9byatH4Wt1SPYUO3zgsj5";beta|i:1324;

Which is

[var name]|[var type]:[var contents length]:[contents data]; [etc....]

So if you had a user id value set as $_SESSION['user_id'] = 45; this would be:

user_id|i:45;

In the session. So you would need to search every session you had for this data string.

Please read this question about how to do this

So you would have code like this:

$string = 'user_id|i:".(int)$user_id_to_block;
$session_file_name = null;
foreach (glob('/home/session_folder/*') as $file) {
    $content = file_get_contents("/home/session_folder/".$file);
    if (strpos($content, $string) !== false) {
        $session_file_name = "/home/session_folder/".$file;
    }
}

Once found, you can then delete that session on the server.

if(file_exist($session_file_name)){
    unlink($session_file_name);
}

BUT:

With many sessions this will be very slow and inefficient.

How you Should be doing it

Each page load you should be checking the logged in user is authenticated. Assuming your user details are database driven, every page load you should be checking that the details are genuine.

<?php
session_start();
if($_SESSON['user_id'] > 0){
     /////
     $sql = "SELECT banned FROM users WHERE user_id = :user_id"; 
     /////
     // Etc. etc. 
     $result = get MySQL result;
     if($result['banned'] === 'Y'){
          /***
           * Member is banned. kick them out.
           ***/
           $_SESSION = []; // reset session. 
           header("Location: index.php");
           die(); 
     }
}

UPDATE

If you are using the session ID as an identifier and you know the session id without needing to search for it; simply do this:

FUNCTION back_to_home() {
        mysqli_close($db);

        // save current admin session (optional).
        $admin_session = session_id();
        // get target id.
        $session_id_to_destroy = $_SESSION['visit_user-session_id'];
        // close the current session.
        session_write_close();
        // load the specified target session 
        session_id($session_id_to_destroy);
        // start the target session.
        session_start();
        // clean all session data in target session.
        $_SESSION = [];
        // save and close that session.
        session_write_close();

        // Optional if you need to resume admin session:

        // reload admin session id
        session_id($admin_session);
        // restart admin session. . ..
        session_start();

        // ... 

        // header should go to a specific file. 
        header('Location: ../index.php');
        exit;
    }
2
  • where can I find the Session folder if I use an XAMPP/Apache server? I mean I host my Website on a V-Server and Test the functions Local on Windows.
    – NvrKill
    Commented Jul 31, 2019 at 9:29
  • PHP.ini will state where the session folder is. session.save_path.
    – Martin
    Commented Jul 31, 2019 at 9:34

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