Question: I would like to deny access to visitors to a specific directory if they are not logged in.
And redirect them to a "you are not logged in" .html file.

How can I do this? Is there some way to edit .htaccess with php if logged out?

Answer: 
You can't do this with Apache.

One way to achieve this would be to forward every requests to a php script via a rewrite rule and to serve files via this PHP script.

But this is heavy, if your files are big, this is not a good solution.

Maybe you should look as the [Apache dbd auth mod](http://httpd.apache.org/docs/2.2/mod/mod_authn_dbd.html) (if you have your users in a database). But again, this should not be the job of Apache because it's not good at it.
It is not Apache job, but PHP can do it for you.

On a public-only page, define no session parameters.

On a public login page, define all session parameters.

On a member-only page, start a session as is :

```
    <?php

    session_start();

    // Assuming variables have been initally defined
    if(isset($_SESSION['username']) AND isset($_SESSION['password']))
    {
        echo 'You are now logged in';
            header('Location: sosecretpage.php');
    }

    else
    {
        echo 'You are not logged in. You dont have access to this page';
    }
    ?>

```