How to kill a/all php sessions?

后端 未结 9 1195
暖寄归人
暖寄归人 2020-11-28 10:07

I have a very basic php session login script. I want to force logout of a certain user or force logout of all users.

How can I read all sessions made to my website,

相关标签:
9条回答
  • 2020-11-28 10:18

    Updated - Aug 2012

    This code is based from the official PHP site, and another well written snippet on SO.

    <?php
    // Finds all server sessions
    session_start();
    // Stores in Array
    $_SESSION = array();
    // Swipe via memory
    if (ini_get("session.use_cookies")) {
        // Prepare and swipe cookies
        $params = session_get_cookie_params();
        // clear cookies and sessions
        setcookie(session_name(), '', time() - 42000,
            $params["path"], $params["domain"],
            $params["secure"], $params["httponly"]
        );
    }
    // Just in case.. swipe these values too
    ini_set('session.gc_max_lifetime', 0);
    ini_set('session.gc_probability', 1);
    ini_set('session.gc_divisor', 1);
    // Completely destroy our server sessions..
    session_destroy();
    ?>
    

    Works well. Servers like NGinx you can turn off, clean cache, swipe memory reset, clear logs etc and generally remove temp usage. Even drop the limits of memory.

    0 讨论(0)
  • 2020-11-28 10:20

    Taufik's answer is the best i could find.
    However, you can further modify it
    After authenticating the user and creating the session variables, add these lines:

    $token = "/sess_" . session_id();
    file_put_contents('log/' . $_SESSION['id'] . '.txt', $token);
    

    If you need to force the user to log out during a cronjob or by an admin request:

    $path = session_save_path();
    $file = file_get_contents('log/xxx.txt'); // xxx is user's id
    $url = $path.$file;
    unlink($url);
    
    0 讨论(0)
  • 2020-11-28 10:27

    You can use session_save_path() to find the path where PHP saves the session files, and then delete them using unlink().

    0 讨论(0)
  • 2020-11-28 10:29

    remove all session variables

    session_unset();
    

    destroy the session

    session_destroy();
    
    0 讨论(0)
  • 2020-11-28 10:32

    You could try to force PHP to delete all the sessions by doing

    ini_set('session.gc_max_lifetime', 0);
    ini_set('session.gc_probability', 1);
    ini_set('session.gc_divisor', 1);
    

    That forces PHP to treat all sessions as having a 0-second lifetime, and a 100% probability of getting cleaned up.

    The drawback is that whichever unlucky user runs this first will get a long pause while PHP does cleanup, especially if there's a lot of session files to go through.

    For one particular user, you'd have to add some code to your session handler:

     if ($_SESSION['username'] == 'user to delete') {
         session_destroy();
     }
    

    PHP's garbage collector isn't controllable, so you can't give it parameters such as "delete all sessions except for user X's". It looks strictly at the last-modified/last-accessed timestamps on the session files and compares that to the max_lifetime setting. It doesn't actually process the session data.

    0 讨论(0)
  • 2020-11-28 10:34

    Clearling all sessions at once would require first knowing which session.save_handler is being used to store sessions and locating the session.save_path in order to delete all sessions. For deleting the current session only, refer to the documentation for session_destroy().

    Here are some common examples for deleting all sessions using standard file and memcached save handlers:

    Using file save handler

    foreach(glob(ini_get("session.save_path") . "/*") as $sessionFile) {
        unlink($sessionFile);
    }
    

    Using memcached save handler

    $memcached = new Memcached;
    $memcached->addServers($listOfYourMemcachedSesssionServers);
    
    // Memcached session keys are prefixed with "memc.sess.key." by default
    $sessionKeys = preg_grep("@^memc\.sess\.key\.@", $memcached->getAllKeys());
    $memcached->deleteMulti($sessionKeys);
    

    Of course, you might want to consider only doing this out of band from your normal HTTP client requests, since cleaning up large session storage may take some time and have inadvertent side effects in a normal request life cycle.

    0 讨论(0)
提交回复
热议问题