title: PHP redirect back to previous page
body: <p>What I have done for the login.php page is if a user has logged in, he will be redirected to first.php page.</p>
  
  <pre><code>session_start();
  if(isset($_SESSION['usr']) &amp;&amp; isset($_SESSION['pswd'])){
      header("Location: first.php");
  } 
  </code></pre>
  
  <p>In all other pages, if user hasn't logged in he will be redirected to login.php page.</p>
  
  <pre><code>session_start();
  if(!isset($_SESSION['usr']) || !isset($_SESSION['pswd'])){
      header("Location: login.php");
  } 
  </code></pre>
  
  <p>Here is the problem: is there a way to redirect the user back to where he was from? Say if you are trying to reach second.php while you are not logged in, you will be redirected to login.php page now; once you log in, can you be redirected back to second.php instead of first.php?</p>
  
  <p>I have tried to use <code>$_SERVER['HTTP_REFERER']</code>, but this variable doesn't contain anything; it only contain something if you are here because you have clicked a link.</p>