I've just finished 1.5 hours of research and while I learned a few things, did not find the answer to my question.

Is it possible to use .htaccess to restrict access to one file in a directory of many files with a username and password? If so, could you write me one quick?

thanksomuch!

craig

    I don\'t know how to do it with .htaccess but would do it in PHP if posed with the problem myself. Try something like the following. Since a user can\'t view the source of a PHP file, you don\'t need to worry too much about the actual username and password being stored in the PHP code.

    <?php
    $user = \"open\";
    $pass = \"sesame\";
    // $username and $password are inputed at the bottom
    if (($username == $user) && ($password == $pass)) {
    ?>
    protected page content goes here
    <?php
    } else {
    // if this code is run, the comparisons above must have been false, so show the login form
    // by posting to $PHP_SELF, the page just reloads and processes the new info
    ?>
    <form action=\"<?php echo $PHP_SELF; ?>\" method=\"post\">
    <p>Username: <input type=\"text\" name=\"username\" size=\"15\">
    <p>Password: <input type=\"text\" name=\"password\" size=\"15\">
    <p><input type=\"submit\" name=\"submit\" value=\"Login\">
    </form>
    <?php
    }
    ?>

    This would be the quickest way. Hope it helps!

      By the way, I didn't put in any of those backslashes you see throughout my example. Is there a forum FAQ with instructions for posting code?

        Refer to Apache's documentation, or better try to find an aswer to your question using search system here.
        Anyway, it is pretty simple.

        Luck,
        Zboris

          Z,

          I DID refer the the docs. I found out how to secure a directory, but not a file. If it is so simple, would you please help me out by posting the solution?

            Simple, just make a directory called safe and put the file and a .htaccess file in there.

              Yes, I thought of that, but then you have to go change all the links to that page (which is not impossible, but a pain, nonetheless). I was just hoping there was an easier way. :-\

              craig

                Erik-

                I tried what you said to do, and quickly discovered that the script calls itself already in order to go to different functions. Therefore, I can't insert an authentication part in it quite as easily. Any other ideas?

                  Write a Reply...