title: php how to redirect to last visited page when logged out from session
body: <p>I have a couple of pages protected with a login; <code>add.php</code>, <code>settings.php</code> and <code>archive.php</code>
  At the top of each of these pages i have this code:</p>
  
  <pre><code>// check login
  session_start();
  if(!isset($_SESSION['blog_login'])){
      $_SESSION['last_visited'] = $_SERVER['REQUEST_URI'];
      header("Location: login.php");
      exit();
  }
  </code></pre>
  
  <p>When logging out, the page request goes to <code>logout.php</code>. Logout.php looks like below:</p>
  
  <pre><code>session_start();
  unset($_SESSION['blog_login']);
  header("Location: login.php");
  </code></pre>
  
  <p>When logging in again, i want to go to the page i was before. This is my <code>login.php</code></p>
  
  <pre><code>if ($_SERVER['REQUEST_METHOD'] == 'POST') {
      $error = NULL;
      if(isset($_POST['username'],$_POST['password'])){
          $user = array(
                          "username" =&gt; $admin_name,
                          "password"=&gt; $admin_passw           
                  );
          $username = $_POST['username'];
          $pass = $_POST['password'];
          if($username == $user['username'] &amp;&amp; $pass == $user['password']){
              session_start();
              $_SESSION['blog_login'] = $username;
              header('Location:'.$_SESSION['last_visited']);                      
          }
          else {
              $error = '&lt;div class="alert alert-danger"&gt;Incorrect login data&lt;/div&gt;';
  
          }
  
      }
  }
  </code></pre>
  
  <p>Unfortunately, the <code>header('Location:'.$_SESSION['last_visited']);</code> line does not send me to the previous page i was before logging out. 
  What i am doing wrong here?</p>