Here is one idea!
First, take all of your important PHP code and isolate it into its own set of files. Then take all of that PHP code and put it into a directory structure OUTSIDE of the web server's document root. This way, the files can't be opened and requested DIRECTLY.
Now in your HTML pages, INCLUDE the PHP code you want by doing something like this:
include( "/a/path/outside/document/root/database.php" ) ;
This would include ALL of the code you need for some sort of database transaction and would protect against someone opening the file through a web browser.
If you use a .htaccess file, the browser will prompt the user with a pop-up log in box. If this is what you want, then you are done. However, you might not want this behavior.
Hope this helps.