Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Modify .htaccess files

by cjf (Parson)
on Feb 04, 2002 at 22:38 UTC ( #143311=sourcecode: print w/replies, xml ) Need Help??
Category: Web Stuff
Author/Contact Info /msg cjf
Description: Uses Apache::Htpasswd to add, delete, or change the password of a user in a .htaccess file. User input is web-based using forms, includes an authorization check.
#!/usr/bin/perl -wT

use strict;
use CGI;
use Apache::Htpasswd;

my $q  = new CGI;
my $ht = new Apache::Htpasswd("/home/cjf/httpd/.htaccess");

my $username   = $q->param("username");
my $passwd     = $q->param("password");

my $action     = $q->param("action");
my $user       = $q->param("user");
my $new_passwd = $q->param("new_passwd");
my $old_passwd = $q->param("old_passwd");

if ($new_passwd eq '') {
    error("You didn't enter a password");

my $valid_user = "cjf";
my $valid_passwd = "J10A1P16H8";

if (($username eq $valid_user) && ($passwd eq $valid_passwd)) {

    if ($action eq "add") {
        $ht->htpasswd($user, $new_passwd);
    } elsif ($action eq "change_passwd") {
        $ht->htpasswd($user, $new_passwd, $old_passwd);
    } elsif ($action eq "delete_user") {
    } else {
        error("No action was submitted.");

} else {
    error("Login Incorrect.");

sub error {
    my $error = shift;
    print $q->header("text/html"),
Replies are listed 'Best First'.
Re: Modify .htaccess files
by merlyn (Sage) on Feb 04, 2002 at 23:41 UTC
Re: Modify .htaccess files
by grinder (Bishop) on Feb 05, 2002 at 08:35 UTC
    There is also user_manage written by Lincoln Stein that is worth looking at.

    From that web page....

    For obscure reasons, there isn't a satisfactory remote tool for the Apache Web server that allows authorized users to change their passwords remotely. As a result, the onerous task of managing the password and group files falls on the Webmaster. user_manage was written to fill this need. In addition to its basic role as a password changer, this script allows the Webmaster to add, edit and delete users and groups, all via a form-based interface.

    user_manage handles most variants of the Apache "Basic" and "Digest" authentication scheme. Using one simple interface, it manages each of the following types of password and group files:

    1. Human readable text files.
    2. DBM database files.
    3. Berkeley DB database files.
    4. SQL databases.

    In addition to running as a CGI script, user_manage can be used from the command-line to manipulate users and groups, replacing both dbmmanage and htpasswd with a simple and consistent interface. Furthermore, by allowing you to define security "realms" consisting of paired password and group files, it avoids operator errors that often result in the incorrect password file being modified.

    Many of user_manage's functions will also work with other Unix Web servers, including NCSA httpd, and the CERN server. There is support for Netscape user (but not group) databases. This script has not been tested with Windows NT servers.

    g r i n d e r
    print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u';
Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://143311]
[shmem]: good thing that Sun already took "OpenWindows", otherwise I'd not stop to shudder imagining an "OpenWindows" from MS
[shmem]: more garbage in, more garbage out that would be
[stevieb]: I found that win10 broke a C# library I was using for one project while enhancing tests for a Perl dist, which breaks other Perl dists, and I'm about to throw my hands up on berrybrew. win2k12 broke one thing, win10 breaks something...
[stevieb]: ...unrelated which requires replacing a lot of code and a whole lib. I'm about to go nix only ffs
[shmem]: stevieb: what you're doing sounds afwully complex. Too much for me this evening to provide brighter insight ;-)
[stevieb]: I don't even own a Windows computer. Both my girl and I have a laptop each with Linux. I'm supporting Windows in some of my projects and I can't even guage whether it's worth it or not.
[stevieb]: shmem It's something I desired to have years ago, which is why I took over berrybrew. Cross-platform build/test automation locally, or over the network Test::BrewBuild
[shmem]: sounds good.
[shmem]: but I'm crumbling smaller stones. remember...
[stevieb]: I'm working on it to fatten it up and make it more reliant so I can finalize my Raspberry Pi automated build system for that software :) It's all well and fun, until I try to make it work with Windows lol

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2017-03-28 22:07 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (342 votes). Check out past polls.