how to redirect the user back to desired URL after login page in PHP?



PHP Snippet 1:

session_start();
$_SESSION['redirect_to']="plan-2.php";

PHP Snippet 2:

<a href="plan.php?no=3">Plan 1</a>
<a href="plan.php?no=3">Plan 2</a>
<a href="plan.php?no=3">Plan 3</a> 

PHP Snippet 3:

<?php
 ob_start();
   $getbackURLid=$_GET['no'];
   include "header.php";

   if(!$current_user) { 
     require_login($getbackURLid);
    }
 ob_end_flush();
?>

PHP Snippet 4:

<?php 
  ob_start();
    include "header.php";
    if($current_user) { 
       req_logout(); }
  ob_end_flush();
?>

PHP Snippet 5:

 <form action="authenticate.php" method="POST">
  <label for="email">Email</label><br/>
  <input type"text" class="input" name="username" id="username" />
  <label for="password">Password</label><br/>
  <input name="password" type="password" class="input" id="password"/>
  <input type"hidden" name="url" value="<?php echo $_GET['url'];?>" />
  <input type="submit" value="Sign In" class="submit"/>
 </form>

PHP Snippet 6:

<?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($_POST['url']){
          header("Location: plan.php?no=".$_POST['url']);
          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");
    }
?>

PHP Snippet 7:

// 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($getbackURLid){
if(!$current_user){
       $_SESSION['redirect_to'] = $_SERVER['REQUEST_URI'];
        header('Location: signin.php?url=$getbackURLid');
    exit("You must log in.");
}
}

PHP Snippet 8:

<?php

    function curPageURL() {
        $pageURL = 'http';
        if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
        $pageURL .= "://";
        if ($_SERVER["SERVER_PORT"] != "80") {
            $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
        } else {
            $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
        }
        return $pageURL;
    }

    // Check if the session's user is logged in properly
    $redirect = "";
    if (!$_SESSION['current_user']) {
        $target_page= "http://www.myserver.com/login?sender=" + urlencode(curPageURL());

        echo "<html>";
        echo "  <head>";
        echo "    <script>";
        echo "      window.location = '", $target_page, "';";
        echo "    </script>";
        echo "  </head>";
        echo "  <body></body>";
        echo "</html>"
    } else {
?>

<html>
  <head>
  </head>
  <body>
    <!-- put your page html here -->
  </body>
</html>

<?php
    }
?>