Gossamer Forum
Home : General : Perl Programming :

Deleting from data file...??

Quote Reply
Deleting from data file...??
Hi,

I'm confused as to how I can show or a success message based on whether or not the username was deleted from the data file. Here is a snippet of the code. I just threw the success sub in there as I didn't know how to insert it.

Any pointers would be appreciated.

open(PWD,">$members_file") | | &error('Server Error. Chmod all writable files to 666.');
foreach $user (@users) {
chomp $user;
@all = split(/:/, $user);
if ($all[0] ne $formdata{'username'}) {
print PWD "$user\n";
close(PWD);
last;
&success('The account you entered was deleted.');
#if $all[0] eq $formdata{'username'};
# &error('The username you entered is not in the database. Try again.');
} else {
&error('The username you entered is not in the database. Try again.');
}
}

Thanks,

Tim
Quote Reply
Re: Deleting from data file...?? In reply to
I'm guessing that the user types in a username and that is the one to be deleted.

Code:
open(PWD,"+>$members_file") | | &error('Server Error. Chmod all writable files to 666.');
@users = (<PWD> );
count=0;
foreach $user (@users) {
count++;
chomp $user;
@all = split(/:/, $user);
if ($all[0] ne $formdata{'username'}) {
print PWD "$user\n";
if ($count == ($#users + 1)) {
&error('The username you entered is not in the database. Try again.');
}
}
elsif ($all[0] eq $formdata{'username'}) {
&success('The account you entered was deleted.');
last;
}
}
close(PWD);

------------------
Chris
Quote Reply
Re: Deleting from data file...?? In reply to
Hello,

Thanks for your input.

I added that and it gave a 500 error. I then changed count=0; to $count=0; and count++; to $count++;. Then instead of deleting the specified username it deletes all of them and leaves a blank page in the browser. Here is the script in its entirety:

#!/usr/bin/perl

$password_file = '/home/public_html/custom/data/password.txt';
$members_file = '/home/public_html/custom/data/members.txt';
$url = 'http://www.page.net';
$url2 = 'http://page.net';
$admin_dir = 'http://page.net/custom/admin.cgi';

# number of days the membership lasts
$membershiplength = 30;


$membershiplength = 30;
$expires = time() + 86400 * $membershiplength;
$difference_in_seconds = $expires - time();
$difference_in_days = $difference_in_seconds / 86400;

$time = time();
########
########
########
print "Content-type: text/html\n\n";

&parseForm;


$username = $formdata{'username'};

open(PWD,"$members_file") | | &error('Server Error. Chmod all writable files to 666.');
@users = <PWD>;
close(PWD);

open(PWD,"+>$members_file") | | &error('Server Error. Chmod all writable files to 666.');
@users = (<PWD> );
count=0;
foreach $user (@users) {
count++;
chomp $user;
@all = split(/:/, $user);
if ($all[0] ne $formdata{'username'}) {
print PWD "$user\n";
if ($count == ($#users + 1)) {
&error('The username you entered is not in the database. Try again.');
}
}
elsif ($all[0] eq $formdata{'username'}) {
&success('The account you entered was deleted.');
last;
}
}
close(PWD);

sub parseForm
{
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);

foreach $pair (@pairs)
{
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$formdata{$name} = $value;
}
}

sub success
{
print qq~
<HTML>
<HEAD><TITLE>Congratulations</TITLE></HEAD>
<BODY BGCOLOR="WHITE" TEXT="BLACK">
<FONT FACE="ARIAL">
<FONT SIZE=4>Congratulations!</FONT><P>
$_[0]<P>
</FONT>
</BODY>
</HTML>~;
exit(1);
}

sub error
{
print qq~
<HTML>
<HEAD><TITLE>Error!</TITLE></HEAD>
<BODY BGCOLOR="WHITE" TEXT="BLACK">
<FONT FACE="ARIAL">
<FONT SIZE=4>Error!</FONT><P>
$_[0]<P>
</FONT>
</BODY>
</HTML>~;
exit(1);
}
1;

I changed the paths for security purposes.

Any other ideas would be appreciated.

Thanks,

Tim

[This message has been edited by Tim Mousel (edited January 27, 2000).]