Solved

server access prompt  & account registration

Posted on 1997-04-24
12
143 Views
Last Modified: 2013-12-25
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;
      }
0
Comment
Question by:Shadow041997
  • 8
  • 2
  • 2
12 Comments
 

Author Comment

by:Shadow041997
ID: 1827934
Edited text of question
0
 

Author Comment

by:Shadow041997
ID: 1827935
Edited text of question
0
 

Author Comment

by:Shadow041997
ID: 1827936
Edited text of question
0
 

Author Comment

by:Shadow041997
ID: 1827937
Adjusted points to 210
0
 
LVL 2

Expert Comment

by:Voodoo
ID: 1827938
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!
0
 
LVL 2

Expert Comment

by:Voodoo
ID: 1827939
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...
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:Shadow041997
ID: 1827940
It does nothing when i enter the same name again, just gives the usual output.
0
 

Author Comment

by:Shadow041997
ID: 1827941
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;
 
}
0
 
LVL 2

Accepted Solution

by:
n3mtr earned 210 total points
ID: 1827942
Your trying to read the file in append mode; you can't do that.(">>file") You need to open it for read mode(file) then close it and open it for append mode.  Or you could experiment with the read write mode(+>file) and the seek command.
0
 

Author Comment

by:Shadow041997
ID: 1827943
But what about the part that reads what variable should i use for this   ($c_uname,$passwd) = split(/:/,@lines);is that right ?
0
 
LVL 2

Expert Comment

by:n3mtr
ID: 1827944
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.
0
 

Author Comment

by:Shadow041997
ID: 1827945
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.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Ever wondered how to display how many visitors you have online. In this tutorial I will show you an easy but effective way to display the number of online visitors in WhizBase. In this article I assume you have read my previous articles and know …
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn how to count occurrences of each item in an array.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now