Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP: SESSION partly deleted after header() + exit
    primarykey
    data
    text
    <p>I am having some trouble with setting up a pretty small application. It's going to be a little survey.</p> <p>The form is split into two pages. After submitting the first one, the data is stored in the <code>$_SESSION</code> array with this:</p> <pre><code>save_items(array('my_data' =&gt; $my_data_oject)); </code></pre> <p>The function <code>save_items()</code> looks like this:</p> <pre><code>function save_items(array $array) { foreach ($array as $name =&gt; $item) { $_SESSION[$name] = $item; } } </code></pre> <p>Then I <code>unset($_POST)</code> and redirect like this:</p> <pre><code>header('Location: index.php?action=survey_part2'); exit; </code></pre> <p>My problem is: After redirection, the previously stored login data is still in the <code>$_SESSION</code>, but <code>my_data_object</code> is not. If I avoid the redirection, I can see that <code>my_data_object</code> is stored in the <code>$_SESSION</code> array before the redirection starts. So the combination of <code>header()</code> and <code>exit</code> seems to destroy the session in part. Does anybody know how this can happen?</p> <p>Finally, parts of my controller:</p> <pre><code>&lt;?php error_reporting(E_ALL); session_start(); require_once 'models/functions.php'; require_once 'models/classes.php'; $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : NULL; $view = $action; $language = isset($_REQUEST['lang']) ? $_REQUEST['lang'] : 'de'; switch ($action) { case 'login' : if ((!empty($_POST['email'])) &amp;&amp; (!empty($_POST['password']))) { $user = new User(); $login = $user-&gt;find_user($_POST); if (!empty($login)) { set_message('Welcome ' . $login['Firstname'] . ' ' . $login['Lastname'] . '!'); save_items(array('user_id' =&gt; $login['CID'])); unset($_POST); redirect("index.php?action=survey&amp;lang=de"); //&lt;- works fine. Login is kept, Message is kept. } else { set_message('Please try again.'); unset($_POST); } } else { unset($_POST); set_message('Try again.'); } break; /* ... shortage */ case 'survey' : check_login(); //&lt;- doesn't matter if (empty($_POST)) { /* ... shortage */ } else { /* ... creation of my_data_object + setting one more message */ save_items(array('my_data' =&gt; $my_data_object)); unset($_POST); save_items(array('test' =&gt; 'you see me?')); //&lt;- index.php?action=survey_2 won't get it //var_dump($_SESSION); header('Location: index.php?action=survey_2&amp;lang=de'); //&lt;- does not work. Login is kept in $_SESSION, but not my_data exit; } break; </code></pre> <p>Thanks!</p> <p>This topic is maybe similar to <a href="https://stackoverflow.com/questions/2037316/php-session-destroyed-lost-after-header">that one here</a>, but my <code>$_SESSION</code> is not empty after <code>header()</code>, but partly deleted.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload