1

I've got a 'shoppingcart' page and if they modify any values there and then click on 'order' i want the session to actually store the information before i header to the next page, however it seems that the $_SESSION variable does not save it before starting.

I have tried the following: Solution 1:

$_SESSION['winkelwagen'] = $winkelwagen;
    session_write_close();
header("Location: checkout.php");

Solution 2:

$_SESSION['winkelwagen'] = $winkelwagen;
header("Location: checkout.php");
        exit();

However, neither of these seem to work for me, i have tried looking at the following stackoverflow topics too: PHP: session isn't saving before header redirect Header Not Working With Sessions

however neither of these work, this is my code:

$header = false;
if(isset($_POST['moveForward'])) {
    foreach($winkelwagen as $key => $val) {
        if (isset($_POST['aantal' . $val['artnr']])) {
            if (isset($_POST['verpakking' . $val['artnr']])) {
                $verpakking = $_POST['verpakking' . $val['artnr']];
                $aantal = intval($_POST['aantal' . $val['artnr']]);

                if ($val['amount'] !== $aantal) {
                    $val['amount'] = $aantal;
                    $winkelwagen[$key]['amount'] = $aantal;
                }
                if ($val['verpakking'] !== $verpakking) {
                    $val['verpakking'] = $verpakking;
                    $winkelwagen[$key]['verpakking'] = $verpakking;
                }
                $header = true;
            }
        }
    }
    $_SESSION['winkelwagen'] = $winkelwagen;
    session_write_close();
    if($header) {
        header("Location: checkout.php");
        exit();
    }
}

Any help would be appreciated!

Community
  • 1
  • 1
ThatsAudio
  • 23
  • 1
  • 7
  • 1
    Did you or your framework call [session_start](http://php.net/session_start) before using the session? – Koala Yeung Feb 13 '17 at 09:25
  • Yes, in our header.php that we include, we have a session_start. – ThatsAudio Feb 13 '17 at 09:28
  • 1
    Are you sure $_SESSION['winkelwagen'] = $winkelwagen; line is running ? because you have several if condition and loops , check this – SarangaR Feb 13 '17 at 09:32
  • [This code](http://codepad.org/gDGfO6aB) works in a `php -S localhost:8080 ./raw.php` environment. That means session can survive after a redirection. Should be other issue than session. – Koala Yeung Feb 13 '17 at 09:40
  • It works perfectly if i don't redirect the page though @KoalaYeung – ThatsAudio Feb 13 '17 at 11:48
  • My code snippet works perfectly redirecting the page through. So probably not the issue of session handling itself. I suggest you should reduce your code for debug. Or build up from my snippet step-by-step to find out what's wrong. – Koala Yeung Feb 13 '17 at 12:07

0 Answers0