8
Here’s the thing: 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 was using perfectly the
session_destroy();
until I had to use
$_SESSION['email'] e $_SESSION['senha']
so that the user only had access to the game page if he was logged in.
So now if I use the
session_destroy();
sessions that keep the user logged in are also destroyed and he is redirected to the home page.
I tried to use
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 reset button several times...
Any suggestions?
Look 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
Inside this main if are more than ten sessions to unsetar, I put only three to exemplify 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" ["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" }
What is inside the
$_SESSION
for each user, and what needs to be deleted? It is yes the case to useunset
, but only for the data you don’t need to keep (and not for the entire user session).– bfavaretto
I used unset in all sessions that I need to delete when restarting the game. However, it doesn’t always work immediately. Sometimes I have to keep clicking one, two, three times on the restart button. It gives the impression that by clicking the button I am deleting the sessions. However all sessions that I need to delete are within an if, so if the condition is true (in this case, if the restart button is clicked) all those sessions will be unset.
– I Wanna Know
It is unclear what you are calling "the sessions". There is only one session per user. And within it there is a lot of data. You can [Edit] your question and include the output of
var_dump($_SESSION)
for any user?– bfavaretto
I explained that before using session to log in I was already using sessions in my game. I asked a few questions.
– I Wanna Know
@Iwannaknow would be more logical to simply reset the game variables only instead of destroying the session.
– Bacco
You can split game variables, for example,
$_SESSION['jogo']['palavra']
etc..$_SESSION['jogo']
would be an array. Then when you need to clean just clear this whole key, with everything inside.– bfavaretto
@bfvaretto I put the var dump in the question, are several sessions... Bacco And as zero the game variables? But I already say that many variables are stored in sessions precisely because I cannot lose data when user refresh page.
– I Wanna Know
Clarifying the terminology, the session is one per user (even before logging in, consider the session as the browser window where your site is). For each session multiple variables can be associated, which are called "session variables". What you are calling "sessions" are "session variables".
– bfavaretto
@bfvaretto I read this just now on a forum. Anyway... Responding to your previous comment: if I understand you, I cannot do this because there are sessions that will only be set if others exist, that is, there are sessions that will not always exist. I can’t put them all in one array.
– I Wanna Know
I think you can, just check what’s in
$_SESSION['jogo']
, instead of checking directly in$_SESSION
.– bfavaretto
In my code logic there are things that only happen if a session variable is set, in other cases if it is not. If I create an array with all variables inside I would be setting them all even if they were empty. Or not? Yes, but why did you suggest unsetting everything at once? Is there a bug if you try unsetar several session variables? I think you suggested what you thought was most practical...
– I Wanna Know
Everything indicates that you are doing it in the right way. What I suggested is just a shortcut. If what you’re doing only works sometimes, it’s hard to know why without knowing your code better. For the part that’s posted, you can’t say.
– bfavaretto
Were you the one who wanted to make a game system for crossword? Can take advantage and give a more concrete example?
– Papa Charlie
This "game" is actually to guess the word. But the problem is only in this very part, at the time of giving unset in the sessions. So much so that everything is working perfectly when I use session_destroy();
– I Wanna Know
Does your code allow a full analysis? You can put it in the codepad to make it easier to identify the problem?
– Papa Charlie
Dude, you asked for the code to do an analysis and I felt compelled to find some error that could be causing this and at the end I was forgetting to unsetar one of the session variables. There are so many... I’m sorry if I wasted someone’s time, but the code is all right, it was an oversight of mine alone. Thanks, your reply brings everything and can be useful for a visitor. @Papacharlie
– I Wanna Know
I Wanna Know, arrange
– Papa Charlie