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 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>";
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 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

In reply to Perl to wite and search text file by Ragna

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.