title: Sessions in PHP with JSON and AJAX
body: <p>So, I'm trying to create a log in function for a web app.</p>
  
  <p>The login is handled by a JSON interface. The user enters a username and password which are checked, and if they are correct, the JSON interface sends back a token. This is where the session should start, I guess.</p>
  
  <pre><code>&lt;script&gt;  
    $(document).ready(function(){
     $("#loginForm").validate({
      rules: {
        username: {
          required: true,
          digits: true
        },
        password: {
          required: true
        }
      },
  
      submitHandler: function(form) {
        data = JSON.stringify({
          "jsonrpc": "2.0",
          "method": "login",
          "params": {
            "params": {
              "username": $('#username').val(),
              "password":  $('#password').val()
            }
          }
        });
  
        $.ajax({
          url:"http://domain.local:1234/trip",
          type:"POST",
          crossDomain: true,
          dataType: "json",
          data : data,
          headers: {
            'Content-Type': "application/json; charset=utf-8"
          },
          success: function(data){
            if(data.result.token != null) {
              window.location.href = "functions/login.php?token=" + data.result.token;
            }
  
            else $("#result").html("Invalid username and/or password."); 
          }
        });
        return false;
        }
      });
    });
  &lt;/script&gt;
  </code></pre>
  
  <p>My <code>login.php</code> is where the session handling should occur (right?). If possible at all, I'd like not to use cookies. So far, this is the only thing I've got (this is my first attempt with sessions).</p>
  
  <pre><code>&lt;?php
    session_start();
    $token = $_POST['token'];
  ?&gt;
  </code></pre>
  
  <p>When the user is logged in, I need them to be sent to overview.php. Should this be done from the <code>login.php</code> file when there's a token, or should it be done from <code>index.php</code> instead of heading to <code>login.php</code>?</p>
  
  <p>Also, how do I check if there's an active session on the pages the user visits? I need the token on every page to make new calls to the JSON interface.</p>
  
  <p>Lastly, I when the user needs to log out, I guess I would just head them to <code>logout.php</code> or something like that and end the session there?</p>
  
  <p>Thank you so much for your help!</p>