How to destroy a specific session?

8
It's as follows: I have an application / game that uses sessions to memorize the data that the users have chosen.

Whenever the user restarts the game I need to clear the information, so I was perfectly using the

     session_destroy();

Until I needed to use

$_SESSION['email'] e $_SESSION['senha']

So the user only had access to the game page if logged in.

So now if I use the

session_destroy();

Sessions that keep the logged in user are also destroyed and it is redirected to the home page.

I tried to use

unset(); 

to empty only the sessions that need to be restarted, but then the system does not work properly. Sometimes I have to keep pushing the restart button several times ...

Any suggestions?

See what I'm doing:

      if ($_POST['entrada'] === "ex" ) //primeiro if
      {

          if(isset($_SESSION['palavra']))
           {
              unset($_SESSION['palavra']); 
           }

          if(isset($_SESSION['sessoes']))
          {
           unset($_SESSION['sessoes']); 
          }              
         if(isset($_SESSION['letra']))   
         {
          unset($_SESSION['letra']); 
         }


      }//fecha o primeiro if

Within this main if there are more than ten sessions to unset, I put only three to illustrate what I'm doing.

VAR DUMP no $ _SESSION: array (13) {["background"] = > string (5) "background" ["email"] = > string (25) "[email protected]" ["password"] = > string (8) "deusdeus" ["class"] = > string (7) "input" ["count"] = > int (3) ["pos"] = > int (0) ["pos_2"] = > int (2) ["error"] = > string (1) "v" ["error_1"] = > string (1) "m" ["error_2"] = > string (1) "w" ["error_3"] = > string (1) "x" ["error_4"] = > string (1) "z" ["error_5"] = > string (1) "and"}

    
asked by anonymous 03.09.2014 / 20:28

2 answers

6

Update

You can make subgroups in the session:

$_SESSION['login'] = array( 'email' => '[email protected]' , 'senha' => 'userpassword' );
$_SESSION['games'] = array( 'palavra' => 'Helicóptero' , 'letra' => 'a' );

Your session will be in 2 groups: login data (email, password), and game data (word, letter ...)

[login] => Array('email' => '[email protected]' , 'senha' => 'userpassword')
[games] => Array('palavra' => 'Helicóptero' , 'letra' => 'a' )

You can remove a specific index from 'session games' using unset( $_SESSION['games']['palavra'] ) or to restart the game you remove the full session of the game using unset( $_SESSION['games'] ) , this will keep the user session unchanged.

Removing session indices

Unset destri a session variable, while session_destroy () will destroy all sessions for the user.

unset( $_SESSION['palavra'] );  // irá remover apenas os dados de 'palavra'
session_destroy();  // irá remover todas as sessões do usuário.

I do not know if it supplies your doubt, but I am giving a simple example ...

// criando sessões de login
session_start();
$email = $_SESSION['email'];
$senha = $_SESSION['senha'];

// criando sessões do jogo
$senha = $_SESSION['palavra'];
$senha = $_SESSION['letra'];

// removendo todas as sessões
session_start();
session_destroy();
unset( $_SESSION );

// removendo sessões do jogo
// opção 1)
unset( $_SESSION['palavra'] );
unset( $_SESSION['senha'] );

// opção 2)
$_SESSION['palavra'] = null;
$_SESSION['senha'] = null;

Masta you use as a condition in every session you need to check.

    
03.09.2014 / 23:09
5

You can do this:

session_start();
$tmpemail = $_SESSION['email'];
$tmpsenha = $_SESSION['senha'];
session_destroy();
session_start();
$_SESSION['email'] = $tmpemail;
$_SESSION['senha'] = $tmpsenha;
    
03.09.2014 / 20:50