title: PHP: Session variable lost, session id changes (using Ajax)
body: <p>I'm new to PHP and am having trouble using session. 
  I call a login php-script from javascript using AJAX. There I want to create the session and set a value. </p>
  
  <pre><code>&lt;?php
  ini_set('display_errors', 1);
  session_start();
  header('Content-Type: application/json');
  header('Access-Control-Allow-Origin: *');
  
  $_SESSION['username'] = "username";
  
  ?&gt;
  </code></pre>
  
  <p>I handle the response in javascript and call another php-script again using AJAX. The other file looks like this: </p>
  
  <pre><code>&lt;?php
  ini_set('display_errors', 1);
  session_start();
  header('Access-Control-Allow-Origin: *');
  header('Content-Type: application/json');
  
  $username = $_SESSION['username'];
  ?&gt;
  </code></pre>
  
  <p>But username is null. If I request the session ID in both files, the session ID changed. From the first file a Response Cookie containing a PHPSESSID is sent. Do I have to use this id in the AJAX call calling the second script? </p>
  
  <p>Update: As requested the AJAX-Code: </p>
  
  <pre><code>function callAjax(url, data, successCB, errorCB) {
      $.ajax({
              url: url,
              type: 'post',
              data: data,
              success: successCB,
              error: errorCB
      });
  }
  </code></pre>
  
  <p>Called like: </p>
  
  <pre><code>callAjax(GET_TEMPLATES_PHP_URL, {}, onGetTemplateSuccess, onRessourceRetrievalError);
  </code></pre>
  
  <p>I checked the answers in similar SO question, but they did not help. </p>
  
  <p>Any ideas? Thanks in advance.</p>