title: PHP / Ajax: How to initiate session after successful login (rest of code working)
body: <p>I am new to PHP and hope someone can help me with the following. </p>
  
  <p>I am working on setting up a login page where I am using an onclick event on a button to verify the email and password ("pass") that the user entered (checking if email exists and if password matches). </p>
  
  <p>So far everything works as intended but I couldn't figure out the following: 
  I start a session in my header file as below. Now when I verified a user's email and password ("<code>password correct</code>") <strong>I would like to pass two variables to this session so that on other pages I can see the user is already logged in</strong>. Therefore I wanted to pass a variable "<code>login</code>" as "<code>loggedIn</code>" and the user's email address as "<code>username</code>". </p>
  
  <p>Can someone tell me how I can realise this here ?
  Can I just do this on the ajax.php page when I already verified the user ?</p>
  
  <p><strong>How the session is being started in my header:</strong> </p>
  
  <pre><code>session_start();
  </code></pre>
  
  <p><strong>The Ajax call to verify the user (in jQuery):</strong></p>
  
  <pre><code>$.ajax({        
      type: "post",   
      url: "ajax.php",
      cache: "false",
      data: {
          node: 'loginUser',
          email: email,
          pass: pass
      },
      success: function(data){
          if(data == 'Password correct'){
              // redirect to index page
              window.location.href = baseURL + '/index.php?lang=' + selectedLang
          }else{
              alert(data);
          }
      },
      error: function(){
      }
  });
  </code></pre>
  
  <p><strong>The part on the ajax.php page that verifies the user with the input from the Ajax:</strong> </p>
  
  <pre><code>case "loginUser":
      // login user
      $email = $_POST["email"];
      $pass = $_POST["pass"]; 
  
      $stmt = $conn-&gt;prepare("SELECT pw FROM Users WHERE email = ? LIMIT 1");
      $stmt-&gt;bind_param('s', $email);
      $stmt-&gt;execute();
      $result = $stmt-&gt;get_result();          
      if(!$result || !$result-&gt;num_rows){
          echo "Email has not been registered yet";
      }else{      
          $passHashed = $result-&gt;fetch_assoc();
          if(password_verify($pass, $passHashed["pw"])){
              echo "Password correct";
          }else{
              echo "Password incorrect";
          }
      }
      break;
  </code></pre>
  
  <p>Many thanks in advance for any help with this. </p>