title: Redirect user to another page after login auth and if statement with PHP PDO
body: <p>I am trying to redirect the user to another page when login is successful and <code>$_SESSION[ 'logged_in' ] = true;</code></p>
  
  <p>However, current code doesn't seem to be working for whatever, everything seems okay logically. I am fairly new to php about two weeks so obviously I'm missing something. I've been looking around to see what I am doing wrong but still can't figure it out, so I am posting here as a last resort. I appreciate you taking the time to view my question.</p>
  
  <p>This is my <strong>login.php</strong></p>
  
  <pre><code>&lt;?php 
  session_start();
  //I have this on every single page. 
  
  include ( 'PasswordHash.php' );
  //I use this for password hashing
  ?&gt;
  
  &lt;html&gt;
  
  &lt;head&gt;
  
  
   &lt;link rel="stylesheet" type="text/css" href="style.css"&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  &lt;div&gt;
      &lt;form name="login" action="login.php" method="POST"&gt;
          &lt;label for "username"&gt;Username: &lt;/label&gt;
          &lt;input type="text" name="username"/&gt;&lt;br /&gt;
          &lt;label for "password"&gt;Password: &lt;/label&gt;
          &lt;input type="password" name="password"/&gt;&lt;br /&gt;
          &lt;button type="submit"&gt;Login&lt;/button&gt;
      &lt;/form&gt;
  
  
      &lt;?php
      $username = $_POST[ 'username' ];
      //take username from form
  
  
      //start connection to database
      $user = 'xxxx';
      $pass = 'xxxx';
      $db = new PDO( 'mysql:host=localhost;dbname=mydatabase', $user, $pass );
              //establish a connection to the database
  
      $sql = "SELECT * FROM users WHERE username=:username";
      //select from table users only where the username matches username entered
  
      $query = $db-&gt;prepare( $sql );
      $query-&gt;execute( array( ':username'=&gt;$username ) );
      $results = $query-&gt;fetch( PDO::FETCH_OBJ ); 
      //fetch object in that table
  
      $stored_hash = $results-&gt;password;
      //fetch only password column, this is the hash (used with PHpass)
  
      $password = $_POST[ 'password' ];
              //take password entered in form
      $hash_obj = new PasswordHash( 8, false );
  
      $check = $hash_obj-&gt;CheckPassword( $password, $stored_hash );
              //check the password entered with CheckPassword class in PHpass
  
              // HERE IS WHERE I GET LOST
  
          if ( $check ){ //check if password matches hash
  
             print_r( "This is a valid user" );
             $_SESSION[ 'logged_in' ] = true; //current session is true
  
          } else {
  
             print_r( "Authentication failed, please Try again.");
             $_SESSION[ 'logged_in' ] = false;
                         //current session is false
          }
      ?&gt;
      &lt;?php
      if ( $_SESSION[ 'logged_in' ] == true ){ //okay session is true
              header("Location:http://mywebsite/anotherpage.php"); 
                              //this is supposed to redirect to anotherpage.php
  
          }else {
              echo "You are not logged in&lt;br/&gt;";
                              //if not successfully logged in, echo this message
          }
      ?&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p></p>
  
  <p></p>
  
  <p>Here is my <strong>anotherpage.php</strong>:</p>
  
  <pre><code>&lt;?php 
  session_start();
  ?&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;link rel="stylesheet" type="text/css" href="style.css"&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div&gt;
      &lt;?php
      if ( $_SESSION[ 'logged_in' ] == true ){
            echo "You are logged in&lt;br/&gt;";
          }else {
            echo "You are not logged in&lt;br/&gt;";
          }
      ?&gt;
  &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>