title: How to use http_referer if form field and php script are at the same page?
body: <p>Suppose i am using pure <strong>php</strong>, with no javascript/jquery or ajax.</p>
  
  <p>I have many pages in a website, lets say page1, page2, page3 and page4.
  all of the first three pages have a link to go to page4, to log in.</p>
  
  <p>In page 4 i have a form field, and above I have a <strong>php</strong> script to catch the user input and put the username in a session and after that i want to redirect to the page where the user came from, but the page is not redirecting. 
  Let me put the code.</p>
  
  <pre><code>&lt;?php 
  ob_start();
  session_start();
  
  if(isset($_POST['username'])){
  
  $username = $_POST['username'];
  $_SESSION['username'] = $username;
  if(isset($_SERVER['HTTP_REFERER'])){
  
  $referer = $_SERVER['HTTP_REFERER'];
  header('location: '.$referer);
  
  }
  
  }
  
  
  
  ?&gt;
  
  &lt;form action="page4.php" method="POST"&gt;
  Username: &lt;input type="text" name="username" /&gt;&lt;br/&gt;
  &lt;input type="submit" value="Submit" /&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>I am starting again all page1, page2, page3 with <code>ob_start()</code> and session_start();</p>
  
  <p>If I use a specific page into the header function then it is redirecting, no problem
  for example header (<code>location: page2.php</code>).
  I am guessing the reason is maybe as my form field and the php script are at the same page (page4)
  So how to <strong>redirect dynamically</strong>? User might come from page 1 or page2 or page 3 and after log in i want them back to the specific page they came from.</p>