All you have to do is, on the bottom of each page, say:
$_SESSION['lasturl'] = $_SERVER['REQUEST_URI'];
Then through the execution of each page, the session variable lasturl will be set to whatever the previously viewed page was. (This assumes you are calling session_start() at the top of each page)