Solved

server access prompt  & account registration

Posted on 1997-04-24
12
147 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 

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
 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Renaming multiple file extensions using Powershell 4 62
What is assert.deepEqual? 4 59
Batch File- Finding Drive Description 11 40
Permission on folders 6 21
If you get a (Blue Screen of Death), your system writes a small file called a minidump. Your first step is to make certain your computer is setup to record memory dumps. Right click My Computer, choose properties. Click on the advanced tab, an…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to dynamically set the form action using jQuery.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

763 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