PhantomStar has asked for the wisdom of the Perl Monks concerning the following question:

Greetings,

I have it working to where I can write to a file and read that file, but when I try to use the same file to match it doesn't seem to work.

Here is the writing portion:

<TR><TD valign=top align=center> <INPUT TYPE="RADIO" NAME="efile" VALUE="ban_account.txt"> </TD><TD align=left> <font face=arial size=-1> ban_account.txt -- Account names that are banned. </TD></TR> ########## EDIT IMPORTANR DATA FILES ########## sub edity { $rf="qpwoeirutytiwasonyturieowpq"; } sub edit { $file = $INPUT{'efile'}; $password = $INPUT{'password'}; &checkpassword; open (DAT,"<$path/data/$file"); @headers = <DAT>; #chomp(@headers); close (DAT); print <<EOF; <FORM METHOD=POST ACTION="$cgiurl"> <INPUT TYPE="HIDDEN" NAME="password" VALUE="$password"> <INPUT TYPE="HIDDEN" NAME="efile" VALUE="$file"> <font face=arial><B>Edit the file: <font color=red>$file</FONT></B> <BR><BR><font size=-1>Enter or change all the html you want in the tex +t box below</font><BR><BR> <TEXTAREA NAME="filecontents" ROWS=25 COLS=65 wrap="OFF"> @{[join("",@headers)]} </TEXTAREA> <BR><BR> <INPUT TYPE="SUBMIT" NAME="edit_final" VALUE="Save your changes"> </form> EOF &Footer; exit; }
It seems to write fine, but it does seem like its adding a space to the end of every line in this file.

Here is the match portion, that DOESN'T WORK.

### CHECK BANNED ACCOUNT ### open (ACC, "$path/data/ban_account.txt"); @ban_account = <ACC>; close (ACC); foreach $account_line(@ban_account) { chomp($account_line); if ($INPUT{'account'} =~ /$account_line/) { print <<EOF; <BR> <table cellpadding=5 border=1 cellspacing=0 bgcolor=$table_bg> <TR><TD align=center><font face=$font_face size=$font_size color=$text +_table> YOUR ACCOUNT HAS BEEN BANNED. </TD></TR></TABLE> <BR><BR> EOF &Footer; exit; } }
Please let me know if anyone can see anything wrong with this code.
Thank you,

PhantomStar

Replies are listed 'Best First'.
Re: Need help reading and writing to a file.
by chromatic (Archbishop) on Sep 16, 2001 at 02:29 UTC
    Nothing immediately jumps out at me. Beware of carriage returns some web browsers send in textareas. You might run a transliteration on the value of the "filecontents" field:

    $INPUT{filecontents} =~ tr/\r//d;

    Otherwise, perhaps the best you can do is to add an else clause:

    if ($INPUT{account} eq $account_line) { # print ban message and such } else { warn "($INPUT{account}) didn't match <$account_line>\n"; }
    Wrapping delimiters around variables lets you see if any whitespace has sneaked through.

    (Side note: I'd use a tied hash instead of a flat file. It's much more efficient.)

Re: Need help reading and writing to a file.
by kschwab (Vicar) on Sep 16, 2001 at 02:54 UTC
    Can't help much with your problem, but the following concerns me:
    # html form value <INPUT TYPE="RADIO" NAME="efile" VALUE="ban_account.txt"> # filename taken from the above form value, used in # an open for read, later, perhaps, in an open for writing $file = $INPUT{'efile'}; ... open (DAT,"<$path/data/$file");
    Youch. I do see a check for a password, but you are letting anyone with the password read and write files as whatever user your httpd runs as.

    Consider if someone passes "../../../usr/local/whatever" in the 'efile' form parameter. You may want to take a look at the security of this script.

Re: Need help reading and writing to a file.
by Zecho (Hermit) on Sep 15, 2001 at 16:19 UTC
    >Ignore

    Originally posted as a Categorized Answer.