Home: Perl Programming Help: Intermediate:
Need some help with this



Mel
Deleted

May 22, 2000, 3:59 PM


Views: 2603
Need some help with this

can anyone tell me what's wrong with the following code?

$username = $FORM{'username'};
$password = $FORM{'password'};

open(DATA, "zach");

@data = <DATA>;
while (($username ne "$usrname") and ($password ne "$pwd")) {
$theline = "$_";
chomp($theline);
($usrname, $pwd, $location) =
split(/::/, $theline);
}
print "Location: $location\n\n";


perlkid
stranger

May 24, 2000, 1:01 AM


Views: 2602
Re: Need some help with this

 
What Type of error are you getting and what are you using this code for? I know a few different methods if your trying to do what I think you are.

Perlkid


Mel
Deleted

May 24, 2000, 12:49 PM


Views: 2602
Re: Need some help with this

I am getting a 500 server error. What i am trying to do is write something that will take a username and password that a user enters and then open a file and go to a certain url for the username/password that the pers entered.

Thanks


TheGame+
Deleted

May 24, 2000, 1:17 PM


Views: 2602
Re: Need some help with this

Your script is seriously garbled - I can only hope this is just a bad cut & paste, and not the original script...

Anyway, assuming the users, passes and URLs are in the file 'zach', and that that file happens to be located in the current directory where the script is executed (who knows, you might get lucky), here's a slightly better script :

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

use CGI qw/:standard/;
use CGI::Carp qw(fatalsToBrowser);
$username = param('username');
$password = param('password');
$url = "http://some.default.url/if_they're_wong/";

open(DATA,"<zach") &#0124; &#0124; die "Sorry, I can't open file 'zach' - $!\n";
while (<DATA> ) {
chomp;
next if /^$/; # skip empty lines
($user, $pwd, $location) = split(/::/);
if (($user eq $username) && ($pwd eq $password)) {
$url = $location;
last;
}
}
close(DATA);
print redirect($url);
</pre><HR></BLOCKQUOTE>

An absolute path to your 'zach' file would help if you get the "Sorry, I can't open..." error.


perlkid
stranger

May 24, 2000, 9:13 PM


Views: 2602
Re: Need some help with this

 
I agree with TheGame Mel.

perlkid


Mel
Deleted

May 25, 2000, 12:37 PM


Views: 2602
Re: Need some help with this

TheGame,
just out of curisity, what did you think was so grabled about the code I posted, was it the formatting or what?


TheGame+
Deleted

May 25, 2000, 11:48 PM


Views: 2603
Re: Need some help with this

Do you really want to know ?

OK, you posted this to the 'intermediate' board after all, so I suppose you know some Perl already, and have already written a few scripts Smile

Here are my comments, in decreasing order of importance (at least for me) :

1) this script will loop forever if $username and $password are not empty

You forgot the while (<DATA> ) loop to feed some values to $_.

2) it will print out an empty Location header if the user/pass is NOT correct (or empty)

I don't think the webserver will like that, and I'm sure the user won't like it either...

3) you assume that the script is run in its own directory (which may be true or not), and don't check whether the open succeeded.

4) you don't close the DATA file.

Normally, the system will do that for you when the script finishes executing, but it's not a clean way of handling files.

5) you use an outdated way of dealing with CGI variables - that is, assuming you DO have some routine to fill in the %FORM hash that you didn't show here.


Mel
Deleted

May 26, 2000, 12:36 PM


Views: 2602
Re: Need some help with this

Thanks for your comments, I am always open for them and I like to hear them because they help me to see things that maybe I would'nt have otherwise. I understand what yo're saying in those comments, but, just so you know I understand those problems and that code was just a rough representation of what I was trying to accomplish, also that was just a piece of the pie, I had already taken care of things like the form hash. But, again thanks for your comments.