Link to home
Start Free TrialLog in
Avatar of CYO
CYO

asked on

add a new user to .htaccess file

I need to add a user to .htpasswd and .htaccess file this is what I got so far.

#!/usr/bin/perl
##############################################################################
#user adding program  v1.0
#
#By: Chris Allen
#
#This program will add a user to a .htpasswd file and a .htaccess file
#
##########################################################################

#set variables
$path_to_htpasswd="/opt2/home4/cyo/access/.htpasswd"
$path_to_htaccess="/opt2/home4/cyo/public_html/members/.htaccess"



# adding user to htpasswd file


             #first, read the existing htpasswd file into memory
             open (HTFILE, "<$path_to_htpasswd")
             || die "Can't read htpasswd file - $!";
             while (<HTFILE>) {
               chomp ($_);
               ($htuser, $htcpass) = split (/:/);
               $ht{$htuser} = $htcpass;
             } close (HTFILE);

             #next, you've already read user's form input, and
             #$newuser and $newpasswd are set to the appropriate
             values

             $salt = substr ("$$", 0, 2); #or, make a more random
             salt...
             $ht{$newuser} = crypt ($newuser, $salt);

             #last, write the new htpasswd file.
             # this is the part that is most error-prone
             # you might want to write to a temporary file,
             # then replace the old file.
             # I'm just going to overwrite the old file

             unlink ("$path_to_htpasswd"); # delete
             old file
             open (HTFILE, ">")
             || die "Can't write file - $!";
             while (($htuser, $htcpass) = each (%ht)) {
               print HTFILE "$htuser:$htcpass\n");
             }
             close (HTFILE);



# add user to .htaccess





All I need is how to add user to .htaccess file
ASKER CERTIFIED SOLUTION
Avatar of gracie
gracie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CYO
CYO

ASKER

Do I need to add the user to .htgroup? how?
I'm not sure it's required (just delete that line from .htaccess).

If you find that it is required or if you want it anyway...
it's just a text file.  Here's what it looks like:
~~~~~~~~~~~~~~~~~~~~
auth: sally
auth: tom
~~~~~~~~~~~~~~~~~~~~
So, you would just add the following for each new user:
auth: newuser

Take Care,
Gracie