Re: the issue of not getting a 404:
Here's what I use:
header("HTTP/1.0 404 Not Found");
exit() makes sure nothing else goes out after the 404 header.
Also you must ensure that nothing gets sent to the browser before
the header() call. Look for echo() and print() before the header() calls.