I'd definitely use DB_File for this - zengargoyle's suggestion of Cache::FileCache is not bad either, but a DBM file probably beats it in this case. Something like
use DB_File;
use constant PACKFMT => "LI";
sub exit_limit_enforced { print(<<"EOT"), exit }
Content-type: text/html
<html>
<head>
<title>Traffic control limit reached</title>
</head>
<body>
$_[0]<br />
Your IP: $ip<br />
Last visit: $time<br />
Number of Visits: $visits
</body></html>
EOT
tie my %limit, 'DB_File', './data/rsn_users.dbm';
my $ip = $ENV{REMOTE_ADDR};
my ($time, $visits) = exists $limit{$ip}
? unpack PACKFMT, $limit{$ip}
: (time() - 21, 0);
$visits = 0 if $time < time() - 86400;
exit_limit_enforced("You reloaded too soon!")
if $time > time() - 20;
exit_limit_enforced(
"You are over the allowed number of visits per day!<br />"
. "You may not visit again before" . localtime($time + 86400)
) if $visits > 15;
$limit{$ip} = pack PACKFMT, $time, $visits;
untie %limit;
Makeshifts last the longest.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|