Ragna
01-13-2004, 12:07 PM
Hello everybody.
Am very new to Perl and CGI, and now I desperately need help with two things:
Am trying to write a script, in Perl, to store form data into a text file (personalDetails.dat) on a UNIX server.
Have done so successfully, but need to modify it, and really cannot get my head around it. Here is the 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 personalDetails.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>";
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
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."; }
If anyone can help me I would be incredible thankful!!
Have tried over an over again, and really need help to get it up and running!!
Am very new to Perl and CGI, and now I desperately need help with two things:
Am trying to write a script, in Perl, to store form data into a text file (personalDetails.dat) on a UNIX server.
Have done so successfully, but need to modify it, and really cannot get my head around it. Here is the 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 personalDetails.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>";
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
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."; }
If anyone can help me I would be incredible thankful!!
Have tried over an over again, and really need help to get it up and running!!