- On the page they try to login set a session variable containing the URL of that page.
- Then redirect them to the login page.
- After a successful login get the previous URL from their session and redirect them there.
Have the page that does the redirecting set a session variable that is the URL of that page:
session_start();
if (!$logged_in)
{
$_SESSION['redirect_url'] = $_SERVER['PHP_SELF'];
header('Location: login.php');
exit;
}
Then after a successful login redirect them to that URL:
session_start();
/* Login code goes here */
$redirect_url = (isset($_SESSION['redirect_url'])) ? $_SESSION['redirect_url'] : '/';
unset($_SESSION['redirect_url']);
header("Location: $redirect_url", true, 303);
exit;
The above can be improved upon but this should give you the idea.