php Session - destroy all Sessions and keep only one active

Asked

Viewed 810 times

3

Hello,

I need to destroy all Sesssions when logging out a system except for just one... someone would know how to implement this with php?

For example:

  • Let’s say I have 5 active sesssions when the user is logged in:

    $session1 = $_SESSION['session1'];
    $session2 = $_SESSION['session2'];
    $session3 = $_SESSION['session3'];
    $session4 = $_SESSION['session4'];
    $session5 = $_SESSION['session5'];
    
  • When logging out, I want to destroy 4 of these Sesssions, but there is one that I need to keep active... how would I look in this part? I tried so:

    session_start();
    
    if(!isset($_SESSION['session4'])){
    
          session_destroy(); 
    
    }//end if
    
    • so it didn’t work out... as it would be right?

3 answers

7

You could create a variable temporarily to receive the session variable value, destroy all and then create only the session variable you want.

$auxiliar = $_SESSION['session4'];

session_unset();

$_SESSION['session4'] = $auxiliar;

Another, more direct way to do this:

$_SESSION = ['session4' => $_SESSION['session4']]
  • I will test, but where you put sesison_unset(), it would be session_destroy() so that all would be destroyed, right? And when that happens, the Temporal Session will not be destroyed together?

  • session_destroy() will destroy the $_SESSION maid. sesison_unset() will destroy the variables of $_SESSION it’s like you’re doing unset($_SESSION["varname"])

2


You are misreading $_SESSION represents the current session, is an associative array with session data, $_SESSION['session1'] represents variable saved in that session.

to delete a session variable you can do

unset($_SESSION['session1']);

to eliminate all variables

$_SESSION = array();

to delete all variables except variable with session1 key'

foreach ($_SESSION as $key => $value) {

    if ($key != 'session1') {

        unset($_SESSION[$key]);

    }
}
  • I understand how to destroy them all and how to destroy them one by one. The point is, imagine we have 20sessions... 19s need to be destroyed and only one needs to be maintained.. want a solution where code works in favor of destroying all and making an if for only one to be active, without the need to unset for each one...

  • 1

    See the response update

  • it seemed to be correct, but I was here testing and it didn’t work... I put it in php Sand box to share: http://sandbox.onlinephpfunctions.com/code/cabb41dce09f6e360af8cb2885e02b9cb0f6fbf8

  • In PHP when you run $session1 = $_SESSION['session1']; you are saving a copy of the value and therefore any change $_SESSION['session1'] will not affect $session1 they do not point to the same value but are not the same variable'

  • 1

    In case the example is correct if you run print_r($_SESSION); after the is will check that the other variables were removed from session ($_SESSION)

  • I performed one more test waiting for the server response time. With this example was solved. Perfect @Jorge Costa

Show 1 more comment

0

You giving session_destroy(); you will destroy all the Session, for you to destroy only the specifics use unset();

therefore:

unset($_SESSION['session1'], $_SESSION['session2']);

RESULT: you will destroy the 2 Session (or the ones you put inside the unset(); ).

  • 1

    "destroy all but one" means that he does not want to specify which he wants to delete, but rather specify which he wants to keep - opposite of what he put in the answer.

  • @Ewerson S. I understand how to destroy them all and how to destroy them one by one. The point is, imagine we have 20sessions... 19s need to be destroyed and only one needs to be maintained.. want a solution where code works in favor of destroying all and making an if for only one to be active, without the need to unset for each one...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.