Problems verifying user name input on a flat file to create a user

I am working on user login and I am having problems with the user creation part. My problem is that I am trying to check the input username for a text file to see if that username exists. I can't get him to compare the input username with the array I brought in. I tried two different ways to accomplish this. One of them uses an array, and the other uses something that I read on the Internet, which I do not quite understand. Any help or explanation would be appreciated.

Here is my attempt to use an array to compare http://codepad.org/G7xmsf3z

Here is my second attempt at http://codepad.org/SbeqmdbG

+5
source share
3 answers

In the first attempt, try to set the if inside the loop:

foreach my $pair(@incomingarray) {
    (my $name,my $value) = split (/:/, $pair);

    if ($name eq $username) {
      print p("Username is already taken, try again");
      close(YYY);
      print end_html();
    }
    else {
      open(YYY, ">>password.txt");
      print YYY $username.":".$hashpass."\n";
      print p("Your account has been created sucessfully");
     close(YYY);
     print end_html();
   }
}

In your second attempt, I think you should try and change the line:

if (%users eq $username) {

with this:

if (defined $users{$username}) {
+2
source

As mentioned above regarding locking a flatfile from other processes, there is a problem with scaling too. the more users you will have a slower search.

I started many years ago with a flat file, believing that I would never scale enough to require a real database and would not want to learn how to use mySQL, for example. In the end, after file failures and long search queries, I had no choice but to go to the database.

, .., . Flatfile .

.

+1

, flatfile . , , , !

, grep(), " ":

if (grep /^$username:/, @incomingarray) {
    print "user name '$username' is already registered, try again\n";
}
else {
    print "user name '$username' is not already registered\n";
}

.

You should always prefer lexical (mine) variables over batch (ours) variables. Why do you think (mistakenly) that $ name and $ username cannot be lexical variables?

You should always use the 3-arg open () form and check its return value, as in the example with your second code. Your open () in the 1st code example is how it was done many years ago.

0
source

All Articles