How to destroy a specific session?
Is this: I have an application / game that uses Sessions to memorize the data that users have chosen.
Whenever the user restarts the game I need to reset the information, so I was perfectly using the
session_destroy();
Until I needed to use
$_SESSION['email'] e $_SESSION['senha']
So that the user only had access to the game page if they were logged in.
So now if I use the
session_destroy();
The sessions that keep the user logged in are also destroyed and it is redirected to the homepage.
I tried using
unset();
To empty only the sessions I need to restart, but then the system does not work properly. Sometimes I have to keep pressing 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 are more than ten sessions to unsetar, I put only three to exemplify what I am doing.
VAR DUMP no $_SESSION: array(13) { ["deeper"]=> string(5) "bottom" ["email"]=> string(25) "[email protected]" ["password"]=> string(8) "deusdeus" ["class"]=> string(7) "entry in the" ["count"]=> int(3) ["avg"]=> int(0) ["pos_2"]=> int(2) ["error"]=> string(1) "t" ["erro_1"]=> string(1) "m" ["erro_2"]=> string(1) "w" ["erro_3"]=> string(1) "x" ["erro_4"]=> string(1) "z" ["erro_5"]=> string(1) "y" }
2 answers
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 the 'games session' using unset( $_SESSION['games']['palavra'] )
or to restart the game you remove the full game session using unset( $_SESSION['games'] )
, this will keep the user session unchanged.
Removing indexes from session
Unset unset 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 fills 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 condition in each session you need to check.
You can do like this:
session_start();
$tmpemail = $_SESSION['email'];
$tmpsenha = $_SESSION['senha'];
session_destroy();
session_start();
$_SESSION['email'] = $tmpemail;
$_SESSION['senha'] = $tmpsenha;