You can do this with moderate success using the server variable $_SERVER['HTTP_REFERER'], which contains the address of the page from which the user arrived. (Note that "REFERER" is missing an "R" .. this is a decade-old spelling quirk of the HTTP spec, or so I'm told).

Anyway, it's important to note that while this is useful, it's not foolproof. There are plenty of ways to spoof the referer/referrer, but unless you're handling sensitive data you probably don't need to worry about it.

Yes, something like that, except that $_SERVER['HTTP_REFERER'] (note: the single-quotes around HTTP_REFERER are required) returns the whole address, not just the filename. I recommend that you get adventurous and echo it yourself to see what it looks like. You can use parse_url() to get the relevant parts of the URL.