How do I modify it to check first, before it writes to the text file, if the username or password has already been registered by someone else? (Neither can be used more than once.) Second question is: I have a login page for excisting users. How do I modify script below to check through the entire text file when a username and password is entered? I think I need a loop at first if statement that caters for all possiblities(password do not match with username, etc) Here is the second script:#!/usr/bin/perl # Program to write data to a text file use CGI qw(:standard); use CGI::Carp(fatalsToBrowser); $firstname = param("firstname"); $lastname = param("lastname"); $email= param("email"); $country = param("country"); $username= param("username"); $password= param("password"); $rpassword = param("rpassword"); print "Content-type: text/html\n\n"; print "<html>\n"; print "<head>\n<title>CGI Script</title></head>\n"; print "<body>\n"; open(TEXTWRITE,">>personalDetails.dat")||die("Can't open personalDetai +ls.dat: $!"); print TEXTWRITE " $firstname"; print TEXTWRITE ","; print TEXTWRITE " $lastname"; print TEXTWRITE ","; print TEXTWRITE " $email"; print TEXTWRITE ","; print TEXTWRITE " $country "; print TEXTWRITE ","; print TEXTWRITE " $username"; print TEXTWRITE ","; print TEXTWRITE " $password"; print TEXTWRITE ","; print TEXTWRITE " $rpassword\n"; close(TEXTWRITE); print " Hello, $firstname $lastname.\n"; print "</body>\n</html>";
If anyone can help me I would be so thankful!! I am sorry this is so basic, and probably a silly question, but I have tried over and over again, and really need help to get it up and running!! Thanks again#!/usr/bin/perl use CGI qw(:standard); $testUsername = param("username"); $testPassword = param("password"); open (FILE, "password.txt") || die "File cannot be opened"; while($line = <FILE> ) { chomp $line; # removes carriage return ($username, $password) = split(", ", $line); if ($testUsername eq $username) { $userVerified = 1; if ($testPassword eq $password) { $passwordVerified = 1; last; # go to the last line }}} close (FILE); print "Content-type: text/html\n\n"; print "<head>\n<title></title></head>\n”; if ($userVerified &&$passwordVerified) { accessGranted(); } elsif ($userVerified && !$passwordVerified) { wrongPassword(); } else {accessDenied(); } sub accessGranted { print "Permission has been granted, $username."; } sub wrongPassword { print "You entered invalid password."; } sub accessDenied { print "You have been denied access to the server."; }
In reply to Perl to wite and search text file by Ragna
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |