Hey i need an htaccess file to like run a file every time that directory is accessed. Or can i put php code inside a htaccess file?
Reason:
I have to make a directory secure for 'members only'. But editing .htpasswd files is too slow. So i need to be able to password protect directorys. WITH OUT including any php on the files themselves.

Is there a way to use just mysql, php, and an htaccess file? All the users info is in mysql and i don't want to move it.

Thanks,
Jared

    6 months later

    Old thread, but I thought I would post that I just accomplished this task and for such a simple thing, it was difficult finding anything on it. So in hopes of helping anyone else...here is some code from novice to novice.

    I include this on my membersonly index page so that the htpasswd file is updated everytime the page is loaded. This pulls only the users I have that are listed in groups 5 or 6, myrow[2] is the username and myrow[3] is their password.

    <?php
    $fp = @fopen ("/home/website/www/membersonly/.htpasswd", "w+");
    $db=mysql_connect(localhost, USER, PASS);
    mysql_select_db(DATABASE, $db);
    $result=@("select * from user where ((usergroupid='5') or (usergroupid='6'))", $db);
    while ($myrow=@mysql_fetch_row($result)) {
    $user = $myrow[2];
    $pass = crypt($myrow[3]);
    $userpass = ("$user:$pass\n");
    @fwrite ($fp,"$userpass");
    }
    @fclose ($fp);
    ?>

    Works great, though may be messy code 😃
    Hope this posts ok, my first post here.

    Good luck.

      2 months later

      Hi thanks for your htpasswd script, I have a question do I need to create a user table with columns user and password, and where are you getting usergroupid where is it from Please clarify on this thanks

        I have the vBulletin forum software, and you can place the members of the forum into user groups that can access certain parts of the forums, ie Amin = usergroup 1 and Mods = usergroup 2, so I am pulling the username and password from specific usergroups (5 and 6 in the example) and adding them to the htaccess file.

        So I have three fields in the database that are used, username, pass, and groupid. But you could use just a username and pass fields if all your database of people will have access.

        Hope that helps.

          10 years later

          hi my friend
          I have a problem , how can i remove WWW from my address i mean how can i create direct link to http://example.com/
          with out www
          tnx

            Of course it does.

            $url = 'http://www.example.com/path/to/myfile';
            $new_url = str_replace('www', '', $url);
            
            echo "Your new URL is: $new_url"; // Your new URL is: http://example.com/path/to/myfile
              Write a Reply...