?
Solved

Perl Simple Login Script

Posted on 2003-03-21
8
Medium Priority
?
457 Views
Last Modified: 2008-03-08
Hi,
im making a perl login script... i want it to check a file... and each line of the file will look like this:
username, password

anyways i enter the right username and password and it always says its wrong or right even if the user is right and password is right ... or user is right and pass is wrong... or some other stuff...
here is my code already::




#!/usr/bin/perl -w
#use strict;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
my $q = new CGI;
print $q->header();



my ($user) = ($q->param("username"));
my ($pass) = ($q->param("password"));
print "$user ";
print "$pass
";

open(login, "login.txt") || die "Cant Open Login.txt";
my $line;
my @lines = login>;
while(my $line = pop @lines){
($uname, $pword)=split(/\,/, $line);

}

print "$uname";
print "$pword";


if( ($user eq $uname) && ($pass eq $pword) ){

print "Welcome, $user: to Area 51 RPG
<hr>";
}else{
print "sorry you have entered a wrong user or pass";
}
#}




its all right... what can be the problem
0
Comment
Question by:markyello
[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
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 2

Expert Comment

by:Itatsumaki
ID: 8185074
# Your check is outside the while loop.  You need to do
# this, I think.  Post output details if this doesn't work
# for you.  Also, in general more details needed to give
# you good answers on problems.


#!/usr/bin/perl -w

#use strict;
use CGI;
use CGI::Carp qw(fatalsToBrowser);

my $q = new CGI;

print $q->header();

my $user = ($q->param("username"));
my $pass = ($q->param("password"));
print "$user ";
print "$pass ";

open(LOGIN, "<login.txt") || die "Cant Open Login.txt";

my $valid_user = 0;

while(<LOGIN>) {
   $user1, $pass1) = split(/\,/);
   if (($user eq $user1) && ($pass eq $pass1)) {
       $valid_user = 1;
       next();
       }
   }

if ($valid_user) {
    print "Welcome, $user: to Area 51 RPG <hr>";
    }
else {
    print "sorry you have entered a wrong user or pass";
    }
0
 
LVL 48

Expert Comment

by:Tintin
ID: 8185603
#!/usr/bin/perl -w
use strict;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
my $q = new CGI;

print $q->header;

my $user = $q->param("username");
my $pass = $q->param("password");

open LOGIN, "login.txt" or die "Can't Open login.txt $!\n";

my $valid=0;

while (<LOGIN>) {
  chomp;
  my ($uname, $pword) = split(/,/);
 
  if ($user eq $uname and $pass eq $pword) {
     $valid=1;
     last;
  }
}

close LOGIN;


if ($valid) {
   print "Welcome, $user: to Area 51 RPG <hr>\n";
}
else {
   print "sorry you have entered a wrong user or pass\n";
}

0
 

Author Comment

by:markyello
ID: 8187101
================================================
i have another question...
in pattern matching..
i have a search, adn i want it to not be case sensititve...
like when someone types in "Computer", or "computer"
it should bring up the same amount of searches...
but it doesnt, "computer, (lowercase only brings up a few), while uppercase Computer brings up alot more...
is there some sign or something to not use case sensitive...
Thanks
====================
also the login still doesnt work
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 20

Expert Comment

by:jmcg
ID: 8187124
Come on, Mark, you've been using the site long enough to know better than to throw in an unrelated question. Start a new question when you have a new topic. (Case-insensitivity in matches is turned on by placing an "i" modifier at the end.)

================

As to your login still not working, there were two relevant modifications: the chomp was needed to clean off the end-of-line for the password and moving the test inside a loop that was processing all of the entries in the login.txt file. Tintin's program looks like it should work -- you should compare your current code against what he posted.

If it's still not working, we probably need to see your code again (without the typos that were in the orginal posting, if possible).
0
 

Author Comment

by:markyello
ID: 8220458
thanks guys...
but why when i do this perl  code:

open (stats, "ID.txt")|| die "SORRY CANT OPEN\n";
my ($stats) = (<stats>);
close(stats);
my ($ID) = $stats +1;  
open (save, ">ID.txt")|| die "SORRY CANT OPEN";
select(save);
print "$ID";
close(save);

it doesnt print anything that i put after it, in fact it doesnt do anything that i put after it????
0
 
LVL 48

Expert Comment

by:Tintin
ID: 8221010
When you print to a file, you need to specify a filehandle (BTW, filehandles are normally in uppercase), eg:

print SAVE $ID;

Anyway, the Perl FAQ has a better solution (with locking).

perldoc -q lock

use Fcntl qw(:DEFAULT :flock);
                   sysopen(FH, "numfile", O_RDWR|O_CREAT)       or die "can't open numfile: $!";
                   flock(FH, LOCK_EX)                           or die "can't flock numfile: $!";
                   $num = <FH> || 0;
                   seek(FH, 0, 0)                               or die "can't rewind numfile: $!";
                   truncate(FH, 0)                              or die "can't truncate numfile: $!";
                   (print FH $num+1, "\n")                      or die "can't write numfile: $!";
                   close FH                                     or die "can't close numfile: $!";
0
 
LVL 20

Expert Comment

by:jmcg
ID: 8221303
But it's the "select" statement that directs all future "default" printing to the filehandle "save", even after the "save" filehandle is closed. This is not a useful way to use "select". Use the print operator with the filehandle as a direct object.

print save "$ID\n";

with no "select" and then the rest of your printing won't be messed up.
0
 

Accepted Solution

by:
Teh_Craze earned 80 total points
ID: 8502827
I decided to rewrite your script, and verify it's work-ability, in hopes that it'll work for you.


#!/usr/bin/perl -w
#use strict;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
my $q = new CGI;
print $q->header();



my ($user) = ($q->param("username"));
my ($pass) = ($q->param("password"));
print "$user ";
print "$pass ";

open(login, "login.txt") || die "Cant Open Login.txt";
@lines = <login>;
foreach $item (@lines) {
($uname, $pword) = split(/\, /, $item);
if ( ($user eq $uname) && ($pass eq $pword) ) {
 &found;
 }
}


sub found {
print "Welcome, $user: to Area 51 RPG
<hr>";
} else {
print "sorry you have entered a wrong user or pass";
}

If you find any errors with the script, comment back the output/error it gives you, and I'll fix.

-- Teh_Craze
Austin Crane
Craze@K-Factor.net
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

765 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