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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.