title: How do I redirect to referring page/url after successful login?
body: <p>I'm aware that this topic has been covered before here on Stack, and I have looked at some answers, but I'm still a bit stuck, being fairly new to PHP.  Every page on my website requires a login, and so users are redirected to a login page on page load.  At the top of each page then I have:</p>
  
  <pre><code>&lt;?
  require("log.php");
  include_once("config.php"); 
  include_once("functions.php"); 
  ?&gt;
  </code></pre>
  
  <p>This redirects the user to log.php (with new code added):</p>
  
  <pre><code>&lt;?
  session_name("MyLogin");
  session_start();
  
      if(isset($_SESSION['url'])) 
         $url = $_SESSION['url']; // holds url for last page visited.
      else 
         $url = "index.php"; // default page for
  
  if($_GET['action'] == "login") {
  $conn = mysql_connect("localhost","",""); // your MySQL connection data
  $db = mysql_select_db(""); //put your database name in here 
  $name = $_POST['user'];
  $q_user = mysql_query("SELECT * FROM users WHERE login='$name'");
  
  if (!$q_user) {
      die(mysql_error());
  }
  
  if(mysql_num_rows($q_user) == 1) {
  
  $query = mysql_query("SELECT * FROM users WHERE login='$name'");
  $data = mysql_fetch_array($query);
  if($_POST['pwd'] == $data['password']) { 
  $_SESSION["name"] = $name;
  header("Location: http://monthlymixup.com/$url"); // success page. put the URL you want 
  exit;
  } else {
  header("Location: login.php?login=failed&amp;cause=".urlencode('Wrong Password'));
  exit;
  }
  } else {
  header("Location: login.php?login=failed&amp;cause=".urlencode('Invalid User'));
  exit;
  }
  }
  
  // if the session is not registered
  if(session_is_registered("name") == false) {
  header("Location: login.php");
  }
  
  ?&gt;
  </code></pre>
  
  <p>The login form is contained in login.php.  The code for login.pho relevant to the PHP/log.php is:</p>
  
  <pre><code>&lt;?
  session_start();
  
  if($_GET['login'] == "failed") {
  print $_GET['cause'];
  }
  ?&gt;
  </code></pre>
  
  <p>and</p>
  
  <pre><code>&lt;form name="login_form" id="form" method="post" action="log.php?action=login"&gt;
  </code></pre>
  
  <p>The answer that I came across stated that I should add:</p>
  
  <pre><code>session_start(); // starts the session
  $_SESSION['url'] = $_SERVER['REQUEST_URI'];
  </code></pre>
  
  <p>to the top of each page, which I did, at the top of the page (above "require("log.php");"), and then add:</p>
  
  <pre><code>if(isset($_SESSION['url'])) 
     $url = $_SESSION['url']; // holds url for last page visited.
  else 
     $url = "index.php"; // default page for
  </code></pre>
  
  <p>to my login page, and use the following URL for redirect on successful login:</p>
  
  <pre><code>header("Location: http://example.com/$url"); // perform correct redirect.
  </code></pre>
  
  <p>I am not 100% where the code which stores the referring URL should go, at the top of log.php or login.php.</p>
  
  <p>I have tried adding it to both, but the login page is just looping once I have entered the username and password.</p>
  
  <p>I wonder if someone could help me get this working?</p>
  
  <p>Thanks,</p>
  
  <p>Nick</p>