Funny....I was just about to do the same thing and yes, your way is the way I was going to do it....quick example.....
#!/usr/bin/perl
use CGI qw(:standard);
$q = new CGI;
$action = $q->param('action');
if ($action eq "login") {
&login();
}
else {
&main();
}
sub main {
print header;
print qq|
<form action="script.cgi" method="POST">
<input type="hidden" name="action" value="login">
Username: <input type="text" name="username">
Password: <input type="password" name="password">
<input type="submit" value="Login">
</form>
|;
}
sub login {
$username = $q->param('username');
$password = $q->param('password');
push(@errors, "Please enter a username.") if ($username =~ /^\s*$/);
push(@errors, "Please enter a password.") if ($password =~ /^\s*$/);
push(@errors, "Invalid characters in username.") unless ($username =~ /^\w+$/);
push(@errors, "Invalid characters in password.") unless ($password =~ /^\w+$/);
if ($#errors > -1) {
foreach (@errors) {
$warning .= "<li>$_";
}
&error($warning);
}
my $protected = "http://$username:$password@www.mydomain.com/";
print "Location: $protected\n\n";
}
sub error {
my ($warning) = shift;
print header;
print $warning;
exit();
}
Yes it's not beautiful but it does the trick. I typed it quickly so if it gives any errors then sorry - I probably missed a ; or " or something.
Paul
Installations:
http://wiredon.net/gt/ Support:
http://wiredon.net/forum/