title: Session is saved when page load is triggered by PHP, but not when triggered by Javascript
body: <p>I'm using cURL to login into a site. [Edit: They set a cookie and generate a token. I grab their token. Do some calculations with it and login. That's why keeping the session open is important.] I have a file that loads another one. Here it is in PHP:</p>
  
  <p><strong>file1.php</strong></p>
  
  <pre><code>&lt;?php
  session_start();
  $_SESSION['var'] = $var;
  
  header("Location: file2.php");
  
  ?&gt;
  </code></pre>
  
  <p><strong>file2.php</strong></p>
  
  <pre><code>&lt;?php
  session_start();
  
  $var = $_SESSION['var'];
  
  ?&gt;
  </code></pre>
  
  <p>That works, however, I have to use Javascript to do some calculations and then I pass those variables to file2.php using GET (and there's one variable that's a session variable). So:</p>
  
  <p><strong>file1.php</strong></p>
  
  <pre><code>&lt;?php
  session_start();
  $_SESSION['var'] = $var;
  ?&gt;
  &lt;script type="text/javascript"&gt;
  var var1 = 'car';
  window.location = "file2.php?var1=" + var1;
  &lt;/script&gt;
  </code></pre>
  
  <p><strong>file2.php</strong></p>
  
  <pre><code>&lt;?php
  session_start();
  $var1= $_GET['var1'];
  $var2 = $_SESSION['var'];
  ?&gt;
  </code></pre>
  
  <p>I tried <code>window.location.href</code> instead of <code>window.location</code>
  I also tried using <code>document.cookie</code> and trying to setcookie (with <code>setcookie("PHPSESSID", [string]</code>) to what was stored in PHPSESSID (the other variables - JSESSIONID and <code>session_id</code>- were both NULL). I tried setting it to the entire string.</p>
  
  <p>I tried</p>
  
  <pre><code>session_name('login_session');
  session_set_cookie_params(0, '/');
  session_start();
  </code></pre>
  
  <p>that I found here: <a href="https://stackoverflow.com/a/5438521">https://stackoverflow.com/a/5438521</a>. At the start of the scripts on both pages.</p>
  
  <p>I also tried <code>session_write_close();</code> at the end of the php script in <strong>file1.php</strong>.</p>
  
  <hr>
  
  <p>Update. I've been reading through and thought if I used Ajax/jquery, I might be able to get it to work. I tried:</p>
  
  <p><strong>file1.php</strong></p>
  
  <pre><code>&lt;html&gt;&lt;head&gt;
  &lt;script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"&gt;&lt;/script&gt;
  &lt;/script&gt;
  &lt;/head&gt;&lt;/html&gt;
  
  &lt;?php
  session_start();
  $_SESSION['var'] = $var;
  ?&gt;
  
  &lt;script type="text/javascript"&gt;
  var var1 = 'car';
  
   function runLogin() {  
   $.ajax({  
   &lt;strike&gt;type: "GET",&lt;/strike&gt;
   type: "POST",  
   url: "file2.php",  
   &lt;strike&gt;data: "var="+var1&lt;/strike&gt;
   data: {'var': var1}
   });  
   }
  
   runLogin();
  &lt;/script&gt;
  </code></pre>
  
  <p><strong>file2.php</strong></p>
  
  <pre><code>&lt;?php
  session_start();
  &lt;strike&gt;$var1= $_GET['var1'];&lt;/strike&gt;
  $var1 = $_REQUEST;
  $var2 = $_SESSION['var'];
  ?&gt;
  </code></pre>
  
  <p>But the session was still lost. The first time I ran it I did the above. The second I added <code>include_once('file2.php');</code> in file1.php under session_start() and the opposite in file2.php</p>
  
  <hr>
  
  <p>Solved:</p>
  
  <p><strong>file1.php</strong></p>
  
  <pre><code>&lt;?php
  $session = session_id();
  $_SESSION['var'] = var;
  ?&gt;
  &lt;script type="text/javascript"&gt;
  var var1= "&lt;?=$session ?&gt;";
  
  function runLogin(session) {  
   $.ajax({  
  type: "POST",  
  url: "file2.php",  
  data: {'session': session}
   });  
  }
  
  runLogin(var1);
  
  &lt;/script&gt;
  </code></pre>
  
  <p><strong>file2.php</strong></p>
  
  <pre><code>&lt;?php 
  $posted = $_REQUEST;
  $sew = $posted['session'];
  session_id($sew);
  session_start();
  
  $var = $_SESSION['var'];
  ?&gt;
  </code></pre>
  
  <p>I searched my history but couldn't find the page I found the idea on.</p>