title: Ajax request and SESSION
body: <p>I have a form that the users complete and then press "Submit" button to register.</p>
  
  <p>I wanted to preview the data to the user before the form is submitted, so i used AJAX to do this. The procedure is as follows:</p>
  
  <p>1) User clicks "submit" button (ex. registration.php)</p>
  
  <p>2) JS script, prevents the default action (which is to submit it) and calls another PHP script via AJAX (<strong>registrationAJAX.php</strong>), that:</p>
  
  <p>a) Performs some database changes</p>
  
  <p>b) stores some variables in a <strong>$_SESSION</strong> array</p>
  
  <pre><code>$_SESSION['userID'] = $uID;
  $_SESSION['userEmailAddress'] = $email;
  $_SESSION['hash'] = $hash;
  </code></pre>
  
  <p>c) json_encodes the following array:</p>
  
  <pre><code>$results = array(
  
                     'result'  =&gt; 'success',
                     'message' =&gt; 'registration was successful' 
  
                  );
  
  echo json_encode($results);
  </code></pre>
  
  <p>d) then exits</p>
  
  <pre><code>exit();
  </code></pre>
  
  <p>3) Back to JS script and on AJAX <strong>success</strong>, i check to see whether the result is "success" and then redirect the user to a script that sends an automated email for account verification:</p>
  
  <pre><code>$.ajax({
      type: "POST",
      url: "js/ajax/registrationAJAX.php",
      data: {form data...},
      cache: false,
      success: function(output){
           var outputJSON = $.parseJSON(output);
           var status = outputJSON.result;
  
           if(status == 'success')
           {
               location.href = "https://domain.com/verify.php";
           }
      }
  });
  </code></pre>
  
  <p>The <strong>verify.php</strong> script MUST have access to the $_SESSION variables to be executed correctly.</p>
  
  <pre><code> //the code will only executed if the $_SESSION['hash'] is set:
   if(isset($_SESSION['hash']))
   {
       //SEND EMAIL ACTIVATION EMAIL TO USER
  
       //INFORM THE DATABASE THAT THE EMAIL IS SENT
   }
  </code></pre>
  
  <p>Sometimes i see that some of my users don't receiver the activation email. This will happen if the <strong>verify.php</strong> script doesn't have the $_SESSION variables i stored during registrationAJAX.php and then fail (as shown at the code above).</p>
  
  <p><strong>Question:</strong></p>
  
  <p>1)Is there a possibility that the variables delay to get stored in the $_SESSION and the redirection to <strong>verify.php</strong> is performed with no $_SESSION variables?</p>
  
  <p>2) Does the exit() command cause a problem?</p>