Tengo desarrollado un simple Wizard
, el funcionamiento es como el uso de un tabs
, que solamente oculta y muestra el div
en el paso que se encuentra.
<div id="step1" class="step">
</div>
<div id="step2" class="step">
</div>
<div id="step3" class="step">
</div>
The content is displayed according to the step through var step = <?php echo $step ?>;
if the variable javascript/jquery
has the following information var step = 3;
, the content of the id step3
is going to be displayed, that is, the following:
<div id="step3" class="step">
</div>
To save the steps that the user is currently in and thus avoid going backwards, I am using sessions PHP
to show the user the step where I am left and not start from the beginning, for this I have three files PHP
:
saveTemp.php
<?php
session_start();
$step = isset($_GET['step']) ? $_GET['step'] : 1;
$_SESSION['datos_form'] = $_POST;
include 'php/libs/validate.php';
header('Content-Type: application/json');
$json = array(
'radio' => $radio,
'step' => $step
);
echo json_encode($json);
?>
wizard.php
<?php
//session_start();
if (isset($_GET['p'])) {
session_destroy();
session_start();
}
include 'validate.php';
$r =array(
1 => 'Transferencia Bancaria',
2 => 'PayPal',
3 => 'Tarjeta de credito',
);
?>
validate.php
<?php
$datosForm = (isset($_SESSION['datos_form']) && is_array($_SESSION['datos_form'])) ? $_SESSION['datos_form'] :array();
$sPaso = isset($datosForm['__step__']) ? $datosForm['__step__'] : 1;
$step = isset($step) ? $step : $sPaso;
$radio = isset($datosForm['radio']) ? $datosForm['radio'] : 1;
$_SESSION['datos_form']['__step__'] = $step;
?>
I now recover the saved steps in the following way:
<script type="text/javascript">
var step = <?php echo $step ?>;
var radio = <?php echo $radio ?>;
</script>
The problem that I am presenting is at the end of the purchase, where I am trying to delete the sessions of the steps $step
to restart them at step 1, using the function unset
:
if($payment === 'Completed'){
session_start();
unset($_SESSION['datos_form']);
unset($_SESSION['__step__']);
unset($_SESSION['formid']);
unset($_SESSION['sPaso']);
unset($_SESSION['Pending']);
unset($_SESSION['radio']);
unset($_SESSION['p']);
unset($_SESSION['step']);
unset($step);
unset($radio);
}
I have done one var_dump($_SESSION);
and one print_r($GLOBALS);
obtaining the following information:
[_SESSION] => Array
(
[datos_form] => Array
(
[__step__] => 3
)
[4b228aaae2a6a7ce403bc4ecbc481de6] => ../libro.pdf
[cart] => Array
(
[0] => 11
)
[qty] => Array
(
[0] => 1
)
[formid] => 64da7c62c643f40684f573acffb144eba6bfaf63
[id_user] => 1
)
)
var_dump
array(6) { ["datos_form"]=> array(1) { ["__step__"]=> string(1) "3" }
When I go to step 1 I get the following change:
[datos_form] => Array
(
[__step__] => 1
)
array(6) { ["datos_form"]=> array(1) { ["__step__"]=> string(1) "1" }
When I go to step 2 I get the following change:
[datos_form] => Array
(
[__step__] => 2
)
array(6) { ["datos_form"]=> array(1) { ["__step__"]=> string(1) "2" }
I think what you have wrong is the flow of your application, since as Byro comments in the other answer, simply doing the unset or session_destroy at that point should work. That's why I'll leave you how I could make it work based on your files and certain modifications.
I took the wizzard.php script as a starting point because it is in charge of including the validate.php script and since the latter is the one that creates the variables $step and $radio to use them in the html it seemed convenient to me (this is nothing more for my tests), leaving:
Here it is to mention that I leave the session_start() at the beginning since validate.php uses the sessions so from here I start the session directly and regardless of what happens. I don't know what you indicate with isset($_GET['P']) obviously you want to destroy the session when you have the parameter P but beyond that I don't see the flow at this point. Another detail is that the html calls it index.phtml and from the wizzard I include it at the end, simply so that below when you indicate:
Those $step and $radio variables are already created with the initial values.
Then everything else is the same, unlike pay1.php which was left like this:
Again, at this point, the first thing I do is start the session, destroying it below, adding or removing data from it is something else, I start it because to destroy the session it has to be started, as indicated in the documentation:
session_destroy()
Take into account that it says "current session" so you have to be logged in so that you know which one you are going to destroy.
They also comment that unset should not be used since this is for obsolete php codes:
Now if you look at the flow, it is in the first instance to start the session and create the necessary variables, then the application for each step starts the session and modifies the variables of it, saving the step in which it is located and when it goes to the last step it start the session and if the payment has been completed then destroy the session. In all the scripts where we use the session, the first thing that is there is the initialization of it.
I leave in a repository the code with the instructions to execute it:
wizard and sessions github
I updated the code on github, leaving the pay1.php file:
EDIT: After talking we saw that it was indeed a problem with the application flow, the first thing that index.php does is include validate.php that has the creation of the variables $step and $radio (checking if they exist in the session previously ), then if the step was number 3, the success_paypal.php file was included, which modified the session variable (or did the unset), followed by creating the step and radio javascript variables with the values of the variables $step and $ radio created previously, finally a javascript function was executed that, taking the data from the previous variables, sent a request to saveTemp.php and this in turn created the session variables with the variables received. The topic is in order
Wrong order:
Correct order:
Cheers!
$_SESSION
is a superglobal that does not require the use of the reserved wordglobal
. Makingunset()
a variable global inside a function will only remove the value from the local copy to the function context. To unset a session variable is more precise: