Q:

Redirecting to the last consuled page PHP

I can't get the right solution for all methodes that i tried.
After successful login,the user doesen't use his account for a precise time,he must reconnect.
what i want is when the user reconnects it redirects directly to the last page he consulted and not in index page.
This's my code : 
authentication form : 
     <form action="login.php" method="POST">
         //input login and password
     </form>

Login 
     <?php

       @session_start();

       if ($_POST['url'] == '') 
        {
          $url = "index.php";
        } 
       else 
       {
           $url = $_POST['url'];
       }

       $login = $_POST['login'];
       $password = $_POST['password'];
       $password_hash = hash('sha256', $password);

       try 
        {
     $connexion = new PDO('mysql:host=localhost;dbname=data1', 'data1', 'password');
      $sql_auth = "
                SELECT * 
                FROM users_list 
                WHERE
                login= :login AND password= :password
                LIMIT 1
            ";

$req_login = $connexion -> prepare("$sql_auth");
$req_login -> bindParam(":login", $login, PDO::PARAM_STR);
$req_login -> bindParam(":password", $password_hash, PDO::PARAM_STR);
$req_login -> execute();
$data = $req_login -> fetch();

 } 
   catch(Exception $e)
   {
       die('Erreur : ' . $e -> getMessage());
    }

   if (!$data)
    {
      header("Location: authentication.php");
        exit;

     } 

   else
   {

        $_SESSION['id_user'] = $data_login['id'];
        $_SESSION['login'] = $data_login['login'];
        $_SESSION['first_name'] = $data_login['name'];
        $_SESSION['id'] = $data_login['id'];
        $_SESSION['status'] = $data_login['status'];
        $_SESSION['logged'] = true;

     $req_conn = $connexion -> prepare("SELECT id_user FROM connexion_user     WHERE id_user='".$_SESSION['id']."'");
$req_conn -> execute();
$data_query = $req_conn -> fetch();

if (!$data_query)
{
    $insert_connexion = $connexion -> prepare ("INSERT INTO connexion_user(id_user, last_connexion, count) VALUES('".$_SESSION['id']."',NOW(), 1)");
    $insert_connexion -> execute();
}
else 
{
    $update_connexion = $connexion -> prepare ("UPDATE connexion_user SET last_connexion=NOW(), count=count+1 WHERE id_user='".$_SESSION['id']."'");
    $update_connexion -> execute();
}

header("Location: $url");
exit ;

}
Logout : 
    <?php

     session_start();

     $_SESSION = array();

     if (ini_get("session.use_cookies")) {

         $params = session_get_cookie_params();

         setcookie(session_name(), '', time() - 42000,

         $params["path"], $params["domain"],

         $params["secure"], $params["httponly"]

       );

       }

        session_destroy();

        header("Location: authentication.php"); 

       exit;

       ?>

I just want a simple solution.
Thanks

A:

Returning to the last page is a bit tricky, especially when you destroy the session. But the really simple approach, without having server to remember your last URL, would be to lose header("Location: $url"); at the end of login.php and output this to user:
<html><body><script> history.go(-2); </script></body></html>

If -2 goes too far back in the history, try -1.
Another way would be to try:
<html><body><script> location.href = document.referrer; </script></body></html>

but it might get you back to login page instead of the previous page. Although this line would work without a problem if you were doing authentication using AJAX.
(Note: If someone will want to do this while using AJAX authentication, you might want to check that document.referrer belongs to your domain and not redirecting outside your website).

