Solved

add a new user to .htaccess file

Posted on 1997-06-23
3
1,557 Views
Last Modified: 2012-06-22
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
Comment
Question by:CYO
  • 2
3 Comments
 

Accepted Solution

by:
gracie earned 100 total points
Comment Utility
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
 

Author Comment

by:CYO
Comment Utility
Do I need to add the user to .htgroup? how?
0
 

Expert Comment

by:gracie
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
execute pl/sql block from perl program 3 111
Perl script to parse log and delete the file 17 170
perl script help 12 101
Perl output collect 7 64
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now