I believe the @GinoDeMaria answer works, but it does some unnecessary checks and still makes some calls to assign variables.
USING SESSIONS
Thus, an easier way to do this is:
$_SESSION['2last_url'] = isset($_SESSION['last_url']) ? $_SESSION['last_url'] : null; $_SESSION['last_url'] = $_SERVER['HTTP_REFERER'];
So $_SESSION['2last_url'] contains the value you are looking for.
Now that Gino has said that the user agent does not always set $_SERVER['HTTP_REFERER'] correctly.
USE OF PROHIBITED VALUES IN THE APPLICATION
This method is a little more elegant and can be much more useful. Just tell check_login.php where to redirect the user after logging in.
First, we will indicate the login page to which we need to redirect. Therefore, instead of going to login.php , go to login.php?redirect=http%3A%2F%2Fexample.com%2Fpost%2F1 . (You may notice that the redirect URL looks strange, this is due to the URL encoding. Either use such a tool or check the PHP urlencode () function ).
Now in your login.php you should add a hidden field that contains this value:
<input type="hidden" name="redirect" value="<?php echo $_GET['redirect']; ?>">
Then in check_login.php just get this value:
header("Location: " . $_POST['redirect']);
And there you go.
source share