Several ways,
the easiest is to have a hidden field in your login form that holds the current URL
<input type="hidden" name="goto" value="<?PHP echo $SERVER["PHP_SELF"]."?".$SERVER["QUERY_STRING"]; ?>">
is a good way to start, then on successfull login, redir to $_POST["goto"];
or store it in a session
$SESSION["lastpage"] = $SERVER["PHP_SELF"]."?".$_SERVER["QUERY_STRING"];