title: how to redirect the user back to desired URL after login page in PHP?
body: <p>OK. I have a page called plans.php, inside I have three links (Plan 1, Plan 2, Plan 3). Each link has its own page and it redirects to login page (login.php, which works fine). So if the user is not logged in when they click lets say on "Plan 2" it will force the user to login so they can see the desired page, all depends of what "Plan" the user chooses.</p>
  
  <p>PROBLEM:
  I'm having a hard time redirecting the user back to the "desired Plan (URL)".</p>
  
  <p>Solution:
  If the user chooses "Plan 1 or Plan 2 (whatever plan)" then it will force user to login (I have that working fine), after user logs in successfully the user has to be redirected to their respective "Plan page".</p>
  
  <p>If any is familiar with this issue please help.  </p>
  
  <p>plans.php</p>
  
  <pre><code>&lt;a href="plan-1.php"&gt;Plan 1&lt;/a&gt;
  &lt;a href="plan-2.php"&gt;Plan 2&lt;/a&gt;
  &lt;a href="plan-3.php"&gt;Plan 3&lt;/a&gt; 
  </code></pre>
  
  <hr>
  
  <p>plan-2.php</p>
  
  <pre><code>&lt;?php
   ob_start();
     include "header.php";
  
     if(!$current_user) { 
       require_login();
      }
   ob_end_flush();
  ?&gt;
  </code></pre>
  
  <p>HTML code:
  What the user is going to see after login page.</p>
  
  <pre><code>&lt;p&gt;Hello, you have been redirected to "Plan 2"&lt;/p&gt;
  </code></pre>
  
  <hr>
  
  <p>login.php</p>
  
  <pre><code>&lt;?php 
    ob_start();
      include "header.php";
      if($current_user) { 
         req_logout(); }
    ob_end_flush();
  ?&gt;
  </code></pre>
  
  <p>HTML code:</p>
  
  <pre><code> &lt;form action="authenticate.php" method="POST"&gt;
    &lt;label for="email"&gt;Email&lt;/label&gt;&lt;br/&gt;
    &lt;input type"text" class="input" name="username" id="username" /&gt;
    &lt;label for="password"&gt;Password&lt;/label&gt;&lt;br/&gt;
    &lt;input name="password" type="password" class="input" id="password"/&gt;
    &lt;input type="submit" value="Sign In" class="submit"/&gt;
   &lt;/form&gt;
  </code></pre>
  
  <hr>
  
  <p>This file verifies user credentials where the login form submits to.</p>
  
  <p>authenticate.php</p>
  
  <pre><code>&lt;?php
    session_start();
    require_once "db.php";
    db_connect();
    require_once "auth.php";
  
    $user_id = credentials_valid($_POST['username'], $_POST['password']); 
       if($user_id){
        log_in($user_id);
  
      if($_SESSION['redirect_to']){
            header("Location: " . $_SESSION['redirect_to']);
            unset($_SESSION['redirect_to']);
  
          }else{
           // Default page after user logs in.
            header("Location: manage.php");
      }
      }else{
         header("Location: login.php?error=1");
         exit("You are being redirected");
      }
  ?&gt;
  </code></pre>
  
  <hr>
  
  <p>I have some PHP functions in this file.</p>
  
  <p>auth.php</p>
  
  <pre><code>// Logs into the user $user
  function log_in($user_id){
  $_SESSION['user_id'] = $user_id;
  }
  
  
  // Returns the currently logged in user (if any)
  function current_user(){
  static $current_user;
  if(!$current_user){
      if($_SESSION['user_id']){
          $user_id = intval($_SESSION['user_id']);
          $query = "SELECT *
                    FROM `********`
                    WHERE `id` = $user_id";
  
          $result = mysql_query($query);
          if(mysql_num_rows($result)){
              $current_user = mysql_fetch_assoc($result);
              return $current_user;
          }
      }
  }
   return $current_user;   
  }
  
  // Requires a current user (Restrict Access to Page)
  function require_login(){
  if(!$current_user){
         $_SESSION['redirect_to'] = $_SERVER['REQUEST_URI'];
          header('Location: signin.php');
      exit("You must log in.");
  }
  }
  </code></pre>