Is it possible to get apache to run a PHP script (or any script) everytime a request is made to a dir?

I believe that PHPLib uses .htaccess to set up some classes for sessions everytime a request is made to get a document. I want to use this to check if the user has logged on and a session has been started. This is to password protect an entire dir so no user can get to i.e. pics without having logged in using a form. (this is my idea of authorizing against a MySQL base on an Apache without mod_auth_mysql)

Is it possible?