Link to home
Start Free TrialLog in
Avatar of Shadow041997
Shadow041997

asked on

server access prompt & account registration

Alright, I have a cgi i made but i don't know how to keep people from using the same name. What is the coding for my script to check the password file for the name entered and tell if it is already in use. This is what i have :                open(PASSWD,">>$file");
      @lines = <PASSWD>;      
      ($c_uname,$passwd) = split(/:/,@lines);    
      if ($FORM{'uname'} eq $c_uname) {      
      &error('c_name');
      exit;
      }
Avatar of Shadow041997
Shadow041997

ASKER

Edited text of question
Edited text of question
Edited text of question
Adjusted points to 210
You've read in the whole file, but you now need to go through each element of the @lines array one by one. I'll add the code, once I've locked the question!
Ok, now that you have read in the file, you now need to step through the @lines array, element by element, until one of two conditions occur. You find a successful match, or you reach the end of the array. Once either instance occurs, you can then break out of the loop. The following should do the trick.

-->8--

@lines = <PASSWD>;
$index = 0;        # To access @lines array

# Loop until we exceed number of elements within array
while ($index <= $#lines) {
  # auto increment $index
  ($c_uname,$passwd) = split(/:/,$lines[$index++]);

  # If name matches...
  if ($FORM{'uname'} eq $c_uname) {
    &error('c_name');
    exit;
  }
}

# If it gets here, then no match was found...

-->8--

Hope this helps...
It does nothing when i enter the same name again, just gives the usual output.
This is the script i have made, but i can't seem to get it to check the names already given in the password file.
#!/usr/local/bin/perl
###########################################################################

$file = '/users/yarkn/.htpp';
# Done
#############################################################################

# Parse Form Contents
&parse_form;

# Password Registry
&pass_reg;

# Return HTML Page or Redirect User
&return_html;


sub parse_form {

   if ($ENV{'REQUEST_METHOD'} eq 'GET') {
      # Split the name-value pairs
      @pairs = split(/&/, $ENV{'QUERY_STRING'});
   }
   elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
      # Get the input
      read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
 
      # Split the name-value pairs
      @pairs = split(/&/, $buffer);
   }
   

   foreach $pair (@pairs) {
      ($name, $value) = split(/=/, $pair);
 
      $name =~ tr/+/ /;
      $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

      $value =~ tr/+/ /;
      $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

      $value =~ s/<!--(.|\n)*-->//g;


         if ($FORM{$name} && ($value)) {
          $FORM{$name} = "$FORM{$name}, $value";
       }
         elsif ($value) {
            $FORM{$name} = $value;
         }
     }
   }


sub pass_reg {
     
   
if (!($FORM{'passwd1'} eq $FORM{'passwd2'})) {
      &return_html;
   }    
     
      open(PASSWD,">>$file");
      @lines = <PASSWD>;
      $new_uname = $FORM{'uname'} ;

      $index = 0; # To access @lines array

      # Loop until we exceed number of elements within array
      while ($index <= $lines) {
      # auto increment $index
      ($c_uname,$passwd) = split(/:/,$lines[$index++]);

      # If name matches...
      if ($FORM{'uname'} eq $c_uname) {
      &error('c_name');
      exit;
}
}



    $random = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
    $new_pass = crypt($FORM{'passwd1'} , $random);



    print PASSWD "$new_uname:$new_pass\n";
    close(PASSWD);
    &return_html;
    exit;
}
sub return_html {

print "Content-type: text/html \n\n";
print "<html><body bgcolor='#000000' text='#ffffff'></body>\n";
print "<center> Your Password is \"$FORM{'passwd1'}\". Your Username Is \n";
print "\"$FORM{'uname'}\".\n\n";
print "</html>\n";

}

sub error {
         
   ($error,@error_fields) = @_;

   print "Content-type: text/html\n\n";

  if ($error eq 'c_name') {

  print "<html><body bgcolor='#000000' text='#ffffff'></body>\n";
  print "<center> That Name Has Already been Chosen</html>\n";
 
  exit;
 
}
ASKER CERTIFIED SOLUTION
Avatar of Wayne Leister
Wayne Leister
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
But what about the part that reads what variable should i use for this   ($c_uname,$passwd) = split(/:/,@lines);is that right ?
It looks like voodoo's code should work, but you forgot to put in the pound sign in the while loop. $#lines will give you the total count of lines; that is what you want.  You have $lines.
I would have done it a different way, but it looks like it should work.
I got my code tto work, thanks to everybody who gave me answers. I want to give voodoo and n3mtr the split of the points for all the help, but i can't so n3mtr gets the points.