...

View Full Version : Simple Perl account editing script



HawkEye
09-25-2009, 10:54 PM
I am currently learning Perl to use it for CGI but I'm having a horrible time getting a script I made to work :(

The script should allow a user to edit their password or delete their account. I get an error along the lines of: syntax error on line 70, near 'exit'

I'm sure there are a lot of HTML errors as well but for right now I'm trying to fix the Perl but you can point out anything you see that's wrong if you want. :thumbsup:



#!/usr/local/bin/perl
require "cgi-lib.pl";
&ReadParse;
print &PrintHeader;
$pwfile = "../datafile/pwdataFI.txt";
$tmpfile = "../datafile/pwdataFI.tmp";
$lokfile = "../datafile/pwlockFI.fil";
print "<HTML><BODY>\n";
unless(-e $pwfile)
{
#passwrod file dosn't exist
#print message & shutdown
print << "PrintTag";
<H1>Sorry!</H1>
<P>$pwfile hasn't been uploaded to the proper diectory. Please contact the webmaster.</P>
</BODY></HTML>
PrintTag
exit(0);
}
if($in{'oldname'}eq"" || $in{'oldpw'}eq"")
{
#re-create form and shut down prog
print<<"PrintTag";
<P><B>ERROR:</B> Please type your current password in the spaces provided.</P>
<form action="http://(user).(webplace).net/cgi-bin/changepwFI.cgi" method="post">
<P>Your current username:<BR>
<input type="text" name="oldname" value="$in{'oldname'}"></P>
<P>Your current password:<BR>
<input type="password" name="oldpw" value="$in{'oldpw'}"></P>
<P>Your new password:<BR>
<input type="password" name="newpw1" value="$in{'newpw1'}"></P>
<P>Type Your new AGAIN!:<BR>
<input type="password" name="newpw2" value="$in{'newpw2'}"></P>
<P>
PrintTag
if($in{'delete'} eq "yes")
{
print "<INPUT TYPE=\"checkbox\" NAME=\"delete\" VALUE=\"yes\" CHECKED>\n";
}
else
{
print "<INPUT TYPE=\"checkbox\" NAME=\"delete\" VALUE=\"yes\">\n";
}
print <<"PrintTag";
Check this box to delete yor record</P>
<INPUT TYPE="submit" VALUE="Change">
</FORM></BODY></HTML>
PrintTag
exit(0);
}
if($in{'newpw1'} ne $in{'newpw2'}
{
#re-creat form nd shut down program
print <<"PrintTag";
<P><B>ERROR:</B> You new Passwords did not match! You must type you password exactly the same way twice. Please try again.</P>
<form action="http://(user).html.net/cgi-bin/changepwFI.cgi" method="post">
<P>Your current username:<BR>
<input type="text" name="oldname" value="$in{'oldname'}"></P>
<P>Your current password:<BR>
<input type="password" name="oldpw" value="$in{'oldpw'}"></P>
<P>Your new password:<BR>
<input type="password" name="newpw1" value="$in{'newpw1'}"></P>
<P>Type Your new AGAIN!:<BR>
<input type="password" name="newpw2" value="$in{'newpw2'}"></P>
<input type="checkbox" name="delete" value="yes">
<P>Check this box to delete your record</P>
<input type="submit" value="change">
</FORM></BODY></HTML>
PrintTag
exit(0);
}
if(-e $lokfile)
{
#lock file exists! print message & shutdown prog
print <<"PrintTag";
<H1>Try Again!</H1>
The database is in use.
Please try again later.</P>
</BODY></HTML>
PrintTag
exit(0);
}
open(LOCK_FILE,">$lokfile") || die "Couldn't create $lokfile\n";
open(FILE,"$pwfile") || die "Can't find $pwfile.\n";
@indata = <FILE>;
close(FILE);
open(TEMP,">$tmpfile") || die "Can't creat $tmpfile.\n";
foreach $i(@indata)
{
chop($i);
($username,$password) = split(/\/,$i);
#start of if #2
if($username eq $in{'oldname'} && $password eq $in{'oldpw'} && $in{'delete'} ne "yes")
{
print TEMP "$in{'oldname'}|$in{'newpw1"}\n";
print "<P>Your password has been changed.</P>\n";
} #end of if #2
#start of else if
elseif($username eq $in{'oldname'} && $password eq $in{'oldpw'} && $in{'delete'} eq "yes")
{
print "<P>Your account has bee deleted</P>\n"
} #end of else if
else #start of elsle
{
print TEMP "$i\n";
}#end of else
}#end of first if
close TEMP;
rename($pwfile,$pwfile.".old");
rename($tmpfile,$pwfile);
close(LOCK_FILE);
unlink($lokfile);
print "<P>Thank you!</P>\n";
print "</BODY></HTML>\n";

FishMonger
09-25-2009, 11:52 PM
The error is due to the missing closing parenthesis on line 51.

Change:

if($in{'newpw1'} ne $in{'newpw2'}

To:

if($in{'newpw1'} ne $in{'newpw2'})

Every Perl script you write should include the warnings and strict pragmas.
i.e.,

use strict;
use warnings;

You should replace this depreciated code:

require "cgi-lib.pl";
&ReadParse;
print &PrintHeader;

With this:

use CGI;

my $cgi = CGI->new;
my %in = $cgi->Vars;
print header();

http://search.cpan.org/~lds/CGI.pm-3.48/lib/CGI.pm

HawkEye
09-26-2009, 03:44 AM
Thank you very much! I won't be able to find out if the fix worked for a few days but I will assume it did. As for the depreciated code and the pragmas well when I said new to CGI I meant about a week so I will look in to those.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum