?
Solved

Apache::htpasswd

Posted on 2000-04-05
11
Medium Priority
?
341 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Cloud Training Guides

FREE GUIDES: In-depth and hand-crafted Linux, AWS, OpenStack, DevOps, Azure, and Cloud training guides created by Linux Academy instructors and the community.

 

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 300 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

It is becoming increasingly popular to have a front-page slider on a web site. Nearly every TV website,  magazine or online news has one on their site, and even some e-commerce sites have one. Today you can use sliders with Joomla, WordPress or …
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

762 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