php – How to remove multi-step array session variables from a wizard?

Question:

I have developed a simple Wizard , the operation is like the use of a tabs , which only hides and shows the div in the step that is found.

<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 by var step = <?php echo $step ?>; if the javascript/jquery variable has the following information var step = 3; The content of the id step3 will 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 PHP sessions, to show the user the step where it left off and not start from the beginning, for this I have three PHP files:

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;
?>

Already the saved steps I recover them in the following way:

<script type="text/javascript">
    var step = <?php echo $step ?>;
    var radio = <?php echo $radio ?>;
</script>

The problem I am presenting is at checkout, where I am trying to remove the sessions from the $step steps to restart them to step 1, using the unset function:

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 a var_dump($_SESSION); and a 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 the following change is obtained:

[datos_form] => Array
    (
        [__step__] => 1
    )
array(6) { ["datos_form"]=> array(1) { ["__step__"]=> string(1) "1" }

When I go to step 2 the following change is obtained:

[datos_form] => Array
    (
        [__step__] => 2
    )
array(6) { ["datos_form"]=> array(1) { ["__step__"]=> string(1) "2" }

Answer:

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 is why I 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 $ step and $ radio variables to use them in the html it seemed convenient to me (this is nothing more for my tests), leaving:

<?php
    session_start();
    if (isset($_GET['p'])) {
       // session_destroy();
       // session_start();
    }

    include  'validate.php';

    $r =array(
        1 => 'Transferenasdascia Bancaria',
        2 => 'PayPal',
        3 => 'Tarjeta de credito',
    );

    include 'index.phtml';
?>

Here 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 mean with isset ($ _ GET ['P']) obviously you want to destroy the session when you have the P parameter 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:

<script type="text/javascript">
        var step = <?php echo $step ?>;
        var radio = <?php echo $radio ?>;
</script>

Those variables $ step and $ radius are already created with the initial values.

Then everything else is the same, unlike pay1.php which looks like this:

<?php
session_start();
$payment = 'Completed';
if($payment === 'Completed'){
    session_destroy();
}
?>

Again at this point the first thing I do is start the session, which below is going to destroy it, add or remove data is something else, I start it because to destroy the session it has to be started as indicated in the documentation:

session_destroy ()

session_destroy () destroys all information associated with the current session. It does not destroy any of the global variables associated with the session, nor does it destroy the session cookie. To reuse session variables, call session_start ().

Take into account that it says "current session", so the session must be started 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:

Use only session_unset () for old obsolete code that does not use $ _SESSION.

Now if you look at the flow 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 the same, saving the step it is in and when it goes to the last step it is 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:

wizzard and github sessions

I updated the code in the github, leaving the file pay1.php:

<?php
session_start();
$payment = 'Completed';
if($payment === 'Completed'){
    $_SESSION['datos_form']['__step__'] = 1;
    $_SESSION['datos_form']['radio'] = 1;
}
?>

Así no se elimina toda la sesión

EDIT: After talking we saw that it was indeed a problem with the flow of the application, the first thing index.php does is include validate.php that has the creation of the $ step and $ radio variables (checking if they exist in the session previously ), then if the step was number 3, the success_paypal.php file was included that modified the session variable (or made the unset), followed by the javascript variables step and radius 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 received variables. The topic is in the order, if validate is first executed and creates the variables $ data and $ radio, even if the session is modified with success_paypal.php and those variables were created therefore the javascript variables remain with the previous value that is Step 3 and when sending the request to saveTemp.php the session variables were created again or modified with that step 3.

Wrong order:

+ wizzard.php
++ validate.php (create session variable and $ radio and $ step variables)
+ success_paypal.php (modify session)
+ saveTemp.php (request to store the state of the session with $ radio and $ stpe)

Correct order:

+ success_paypal.php (modify session)
+ wizzard.php
++ validate.php (create session variable and $ radio and $ step variables)
+ saveTemp.php (request to store the state of the session with $ radio and $ stpe)

Greetings!

Scroll to Top