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

I have a script that folks can verify an account number (based on a flat txt file.) I have a failed attempt (account number not found) wright to a log file named their ip address. What I need is code to insert within the below snippet that will see that IP address file 4 times and then give the visitor an error and lock them out from submitting any more account numbers. (maybe disable the submit button?)
my $g = "$siddir/log/$ip_address_log"; if (!open(SIDF, ">$g")) { errexit("Unable create customer session"); + } flock(SIDF, $LOCK_EX) if $LOCK_EX; print SIDF "\n$accid\n"; close(SIDF); exit;
Please email me if you need more detail redacted@example.com Thanks in advance. Lennie Core

Replies are listed 'Best First'.
Re: Count script
by Athanasius (Cardinal) on Jan 25, 2014 at 03:06 UTC

    Some preliminary points:

    1. For future reference, please read Writeup Formatting Tips and enclose your code in <code>...</code> tags.
    2. I don’t know of any Perl command named errexit?
    3. flock is for preserving data integrity in a multithreaded environment. It is not a tool for enforcing security by locking users out. (You may well be on top of this, but from the question asked and the snippet given, it’s hard to be sure.)
    4. Please don’t give your email address, or request private correspondence. That’s not how the Monastery works. Once a thread is started, the discussion is available for everyone to read — and benefit from.

    Now to the question. Create a hash, say %ip, and update it each time a visitor attempts to verify an account number:

    ++$ip{$ip_address};

    Then grant access or not, as follows:

    if ($ip{$ip_address} < 4) { # Grant access } else { # Show an error message }

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,