title: jQuery AJAX request to PHP login not working properly
body: <p>So I'm trying to do an inline login whenever the PHP $_SESSION['logged_in'] variable is not set. (this variable is set when I log a user in) The problem is that I can get my script to work when using multiple pages with header redirects, but trying to remain on the same page via AJAX requests to the login.php file is failing. Here's a look at my code.</p>
  
  <p>the login form itself...</p>
  
  <pre><code>&lt;?php 
        include ('global.inc.php');
          if ( $_SESSION['logged_in'] == 1 ) {
              echo '&lt;form id="addNoteForm"&gt;&lt;label for="title"&gt;Title&lt;/label&gt;&lt;input id="noteTitle" type="text" class="form-control" name="title"/&gt;
                  &lt;label for="content"&gt;Content&lt;/label&gt;&lt;textarea id="noteContent" class="form-control" rows="20" name="content"&gt;&lt;/textarea&gt;
                  &lt;input type="submit" id="addNoteButton" class="btn btn-primary" value="Add Note" /&gt;
                  &lt;button class="btn" onclick="cancelAddNote()"&gt;Cancel&lt;/button&gt;
              &lt;/form&gt;';
          }
          else {
  
              echo '&lt;form id="loginForm" action="login.php" method="get" role="form"&gt;
                  &lt;div class="form-group"&gt;
                  &lt;label for="username"&gt;Username&lt;/label&gt;
                  &lt;input id="loginUsername" type="text" class="form-control" name="username" value="'.$username.'"&gt;
                  &lt;/div&gt;
                  &lt;div class="form-group"&gt;
                  &lt;label for="password"&gt;Password&lt;/label&gt;
                  &lt;input id="loginPassword" type="password" class="form-control" name="password" value="'.$password.'"&gt;
                  &lt;/div&gt;
                  &lt;button type="submit" class="btn btn-default" name="submit-login"&gt;Login&lt;/button&gt;
                  &lt;/form&gt;';
              }
          ?&gt;
  </code></pre>
  
  <p>jQuery AJAX request script</p>
  
  <pre><code>  jQuery(function() {
      jQuery('#loginForm').submit(function(e){
          e.preventDefault();
  
          var username = jQuery('#loginUsername').val();
          var password = jQuery('#loginPassword').val();
  
          jQuery.ajax({
              type: 'GET',
              url: 'login.php',
              data: 'username='+username+'&amp;password='+password,
              success: function(data) {
                  setTimeout(function(){
                     window.location.reload();
                  }, 1000);
  
              },
              error: function(data) {
                  console.log('AJAX login failed. Returned data = '+data);
              }
          });
      });
  });
  </code></pre>
  
  <p>here is the login.php file</p>
  
  <pre><code>&lt;?php
  
          header("Access-Control-Allow-Origin: *");
          require_once (__DIR__.'/includes/global.inc.php');
  
          $userTools = new UserTools();
  
          if ( isset($_GET['username']) ) {
              $username = $_GET['username'];
              $password = $_GET['password'];
          }
  
          if ( $userTools-&gt;login($username, $password) ) {
              return true;
          }
  
          else {
              return false;
          }
      ?&gt;
  </code></pre>
  
  <p>the global.inc.php file</p>
  
  <pre><code>require_once (__DIR__.'/../classes/Note.class.php');
  require_once (__DIR__.'/../classes/NoteTools.class.php');
  require_once (__DIR__.'/../classes/User.class.php');
  require_once (__DIR__.'/../classes/UserTools.class.php');
  require_once (__DIR__.'/../classes/Database.class.php');
  
  
  // connect to the database
  $db = new Database();
  
  // initialize UserTools object
  $userTools = new UserTools();
  
  // start the session
  session_start();
  
  // refresh session variables if already logged in
  if ( isset($_SESSION['logged_in'])) {
      $user = unserialize($_SESSION['user']);
      $_SESSION['user'] = serialize($userTools-&gt;getUser($user-&gt;id));
  }
  </code></pre>
  
  <p>and finally, the login script of my UserTools.class.php</p>
  
  <pre><code>public function login($username, $password) {
  
          $hashedPassword = md5($password);
  
          $database = new Database();
          $database-&gt;query('SELECT * FROM users WHERE username = :username AND password = :password');
  
          $database-&gt;bind(':username', $username);
          $database-&gt;bind(':password', $hashedPassword);
  
          $user = $database-&gt;single();
  
          $user = new User($user);
  
          if ( $database-&gt;rowCount() == 1) {
              $_SESSION['user'] = serialize($user);
              $_SESSION['login_time'] = time();
              $_SESSION['logged_in'] = 1;
              return true;
          }
  
          else {
              return false;
          }
      }
  </code></pre>
  
  <p>I feel like I'm overlooking something minor here and need a fresh set of eyes to take a look at it. Like I said before, when I had a separate login.php file that upon successful login did a header redirect to a "welcome page", I had no problems with it. I think I'm having an issue with how I'm returning data to my jQuery function through AJAX. I dunno. Any help is GREATLY appreciated!</p>