In order for some PHP script to know what the 'previous' page was, there are several ways to go about it. One is to look at the value of $_SERVER['HTTP_REFERER']. The value that PHP puts there is provided by the user's browser and may or may not be defined. I find that it usually is but you cannot guarantee it.
Another way is for you to maintain some kind of page history in session. You could manually store each page in your page history:
// you must always call this before checking $_SESSION values
session_start();
// check to see what the last page is
$histCount = sizeof($_SESSION['pageHistory']);
if ($histCount > 0) {
$lastPage = $_SESSION['pageHistory'][$histCount-1];
} else {
$lastPage = '';
}
// at the end of each script, you would have to make sure that you add it to the page history
$_SESSION['pageHistory'][$histCount] = $_SERVER['PHP_SELF'];
Another way is to change every link in your site so that it defines a variable which tells you that page that brought you here
<a href="somePage.php?lastPage=<?=$_SERVER['PHP_SELF'] ?>">Some page blah blah</a>