You want to prevent the delivery of a file -- that is in sphere of the webserver. I'd suggest you use a .htaccess file protecting a directory, that would be the most simple and safe solution (though it might render your PHP script obsolete).
If you want to have your own login dialogue I can just think of the dirty solution of file copy / delete operations...