• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1664
  • Last Modified:

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
0
CYO
Asked:
CYO
  • 2
1 Solution
 
gracieCommented:
You actually don't.  .htaccess just points to .htpassword
It can also point to a group file ( .htgroup )

Here's what the contents look like:

AuthUserFile /www/admin/.htpasswd
AuthGroupFile /www/admin/.htgroup
AuthName Administration
AuthType Basic

<Limit GET POST PUT>
require group auth
</Limit>

So, once .htaccess is setup, you don't need to modify it for each user.

Enjoy,
Gracie
0
 
CYOAuthor Commented:
Do I need to add the user to .htgroup? how?
0
 
gracieCommented:
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
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now