#!/usr/bin/perl use CGI qw(:standard); my $debug = 0; my $q = new CGI; my $cgi_user = $q->param('user'); my $cgi_pass = $q->param('pass'); # normally, you'd read this stuff from a user database file or DBM. # to make this a single file example, tho, we'll read from the special # DATA handle. man perldata for more info. # open USERS, "users.txt" or die "opening USERS: $!"; print "Content-type: text/plain\n\n", "cgi_user=\"$cgi_user\"\n", "cgi_pass=\"$cgi_pass\"\n" if $debug; my $found = 0; my $password = undef; while () { chomp; my ($user, $pass) = split /:/; print qq[ "$user" => "$pass"\n] if $debug; if ($user eq $cgi_user) { $found = 1; $password = $pass; last; } } # close USERS or die "closing USERS: $!"; my $status; my $good = 0; if (not $found) { $status = "unknown user \"$cgi_user\""; $good = 0; } elsif ($password ne $cgi_pass) { $status = "known user \"$cgi_user\", bad password"; $good = 0; } else { $status = "known user \"$cgi_user\", correct password"; $good = 1; } print header(), start_html("Tkil's CheckPass Sample CGI"), h1("Tkil's CheckPass Sample CGI"), start_form, "user", textfield('user'), br, "pass", textfield('pass'), br, "status: $status", br, "good: $good (", ($good ? "good" : "bad"), ")", br, submit, end_form, end_html; # provide an error code if so desired exit ($good ? 0 : 1); __END__ tkil:xyzzy bob:nothing jim:secret