Solved

Apache::htpasswd

Posted on 2000-04-05
11
337 Views
Last Modified: 2013-12-25
Hi,
 
 Has anyone used the apache::Htpasswd module?
 
 I'm having trouble getting useful error messages.  Here is what the man page says about errors:
 
           # If something fails, check error
           $foo->error;
 
       error;
           If a method returns an error, or a method fails, the error can be retrived by calling error()
 
 I've tried this in various forms, and all I get back is something like this:
 Apache::Htpasswd=HASH(0x80c724c)
 
 When you set a new htpasswd the module is supposed to return 1 or undef, I get a string like
 this: Apache::Htpasswd=HASH(0x818a084)
 
 Those really don't help me put any kind of error testing or
 troubleshooting into my script.  Any help would be appreciated.
 
 Here is a link to the module if anyone would like to try and help me out:
 
 http://www.cpan.org/modules/by-module/Apache/Apache-Htpassword-1.2.readm
 e
 http://www.cpan.org/modules/by-module/Apache/Apache-Htpassword-1.2.tar.g
 z
 
My question:
1.  Is this the best solution, or is there a better way?
2.  Can you give a working example so I can see how it works?
 
 Thanks in advance,
 
 --John
 
0
Comment
Question by:jeacopgh
  • 7
  • 4
11 Comments
 

Author Comment

by:jeacopgh
ID: 2687525
Adjusted points from 30 to 50
0
 

Author Comment

by:jeacopgh
ID: 2687990
Forgot to mention I'm using RedHat Linux 6.1 and Apache/1.3.9.

Thanks.

--John
0
 
LVL 1

Expert Comment

by:mafweb
ID: 2689439
Hi,

to me (not really knowing this module), it looks like $foo->error returns a hash, not a single variable.
You can check this by using:

$err=$foo->error;
foreach $key ($err) {
  print $key.":".$err{$key}.\n;
}

hope this works (haven't used perl for a while ;-()

maf
0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 

Author Comment

by:jeacopgh
ID: 2689782
Hi,

Ok, I tried what maf said, this is the error I got:

Software error:

Can't call method "error" on an undefined value at
/home/httpd/cgi-bin/pwdprot/test2.cgi line 56.


This is line 56:
               $err=$foo->error();


Do I need to add anything else to the program?

Thanks,


--John

0
 
LVL 1

Expert Comment

by:mafweb
ID: 2689838
I'm not sure, but from the docs, it looks like you should use

            $err=$foo->error;

maybe this helps,

maf
0
 

Author Comment

by:jeacopgh
ID: 2689863
Hey,

Yeah, I tried that, too, got the same error:

Software error:

Can't call method "error" on an undefined value at
/home/httpd/cgi-bin/pwdprot/test2.cgi line 56.

Line 56:
                $err=$foo->error;



Thanks,

--John
0
 
LVL 1

Expert Comment

by:mafweb
ID: 2690102
could you please send me the entire program???

maf
0
 

Author Comment

by:jeacopgh
ID: 2690158
Ok, a couple of notes first:

1.  I know it's nasty, I'm not the greatest programmer.

2.  The goal is to create htaccess and htpasswd files the input form on the web.

Thanks maf!

#!/usr/bin/perl
use CGI;
CGI::ReadParse(*in);
use CGI::Carp qw(fatalsToBrowser);
sub BEGIN {
        unshift (@INC, "Htpasswd.pm");
                }
use Apache::Htpasswd;

# get input
$protectdir = $in{'protectdir'};
$authname = $in{'inputtext'};
$username = $in{'username'};
$pwdir = $in{'pwdir'};
$password = $in{'password'};
$password2 = $in{'password2'};

$protectfile = $pwdir . "\.htpasswd";

# make sure password was typed correctly both times
if ($password ne $password2) {
        print "Content-type: text/html\n\n";
        print "<html><body>";
        print "Error: Invalid New Password.";
        print "</body></html>";
        print "\n";
        die "Invalid New password";
   }  
   
open (OUTPUT, ">$protectdir/.htaccess") || die "can't open $protectdir/.htaccess in write mode";
<!-- Out to htaccess file -->
close (OUTPUT);


# touch the htpasswd so it exists
        system("touch $protectfile");

        $newpw = new Apache::Htpasswd("$protectfile");
        $newpw->htpasswd("$username", "$password");

                $err=$foo->error;

if ($err eq 1) {
        print "Content-type: text/html\n\n";
        print "<html><body>";
        print "Success\n<br>";
                foreach $key ($err) {
                print $key.":".$err{$key}.\n;
                }
        print "\n<br>";
        print $newpw;
        print "\n</body></html>\n";
} else {
        print "Content-type: text/html\n\n";
        print "<html><body>";
        print "Error\n<br>";
                foreach $key ($err) {
                print $key.":".$err{$key}.\n;
                }
        print "\n<br>";
        print $newpw;
        print "\n</body></html>\n";
}
0
 
LVL 1

Accepted Solution

by:
mafweb earned 75 total points
ID: 2690268
oops, I think there was kind of a misunderstanding here...
$foo was meant to be replaced by the name of your Htpasswd object.
So the line should read:

        $err=$newpw->error();

or maybe

        $err=$newpw->error;

(as mentioned, I'm not really sure about the () here, but I think the first is right)
What you do here is create a new object
(newpw=new Apache::Htpasswd(..)), tell it to add a new entry
(next line) and finally "ask" it for any errors that might have occured.

hope this helps,

maf
0
 

Author Comment

by:jeacopgh
ID: 2690495
Adjusted points from 50 to 75
0
 

Author Comment

by:jeacopgh
ID: 2690496
maf,

Thanks, that did it.  About 20 more minutes on my part cleaning up the script and fixing the error checking and it's done!

Thanks for the help, for some reason I just couldn't figure that one out.

--John
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Ever wondered how to display how many visitors you have online. In this tutorial I will show you an easy but effective way to display the number of online visitors in WhizBase. In this article I assume you have read my previous articles and know …
Introduction This tutorial will give you a fast look what you can do with WhizBase. I expect you already know how to work with HTML at least, and that you understand the basics of the internet and how the internet works. WhizBase is a server-s…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

828 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