Personally, I'd use a session to store the user's page history as they move through your site. It's useful not just for things like this, but for building up a picture of how people move through your site. If on each page the page appends the current page's URL to, say, $_SESSION['history'], then the login.php page just has to check the last entry, and it knows that's where the user came from. It's invisible to the client, but lets you know exactly how the user got to where they are. If you use an auto_prepend_file, you can put the session history stuff in there, though remember that the login.php file will also count in that history, so it'll have to check for the penultimate entry in the history array, not the last one.