title: Setting session variable with jQuery (without passing via Post or Get)
body: <p>So I have a page where I'm requesting users to enter a key via an <code>&lt;input&gt;</code> tag. I would like to store this information in <code>$_SESSION['sessionKey']</code> when the user clicks a button. Since I'm trying to keep the key moderately secure, I want to <strong>avoid passing this information via GET or POST</strong>. </p>
  
  <p>Most of what I've found online shows this done by using GET/POST and I'm having difficulty finding information on a method that would not use this approach. I did find <a href="https://stackoverflow.com/questions/4365738/how-to-access-php-session-variables-from-jquery-function-in-a-js-file">this question</a>, which suggests to put JavaScript in a file that has an extension of .php, and from there, use the PHP tags to obtain the $_SESSION variables. I followed this approach like so...</p>
  
  <p><strong>javascript.php</strong></p>
  
  <pre><code>&lt;?php
  require ("common/startandvalidate.php");
  ?&gt;
  
  $(document).ready(function() {
      $("#submitButton").click(function(){
          &lt;?php $_SESSION['sessionKey']?&gt; = $("#sessionKeyInput").value;
      });
  });
  </code></pre>
  
  <p><strong>mainPage.php</strong></p>
  
  <pre><code>&lt;head&gt;
     &lt;script src="javascript.php"&gt;&lt;/script&gt;
     &lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  
  &lt;input id="sessionKeyInput" placeholder="Session Key" /&gt;
  &lt;input id="submitButton" value="Submit" type="button" /&gt;
  </code></pre>
  
  <p>When I look at the page through Inspector (in Chrome) I see the error message: </p>
  
  <pre><code>Undefined index: sessionKey
  </code></pre>
  
  <p>the <code>require("common/startandvalidate.php");</code> contains <code>session_start()</code>, so the session is being initiated... </p>
  
  <p>I think it's not working because <code>$_SESSION['sessionKey']</code> has never been declared before, so even though I want to assign a value to it, it's trying to see the variable contents, which is undefined. How could I go about assigning this value to the $_SESSION variable?</p>
  
  <p>Sorry if this is a simple question, any resources are appreciated!</p>