The code I included in my last post is copied from the delete_records subroutine, although edited a bit. It needs to go into the auto_delete routine in order to keep the user from being taken to html_delete_success or html_delete_failure after the records are deleted.
I've been thinking about this and I see a potential problem with putting this into html_home because every time a user goes to the home page it's going to go through the routine. It could slow things down and will definitely be redundant. It might be better to run the subroutine whenever someone logs on, rather than every time they go to the home page. If you get more than one visitor a day, it will still be redundant, but not as bad.
Probably a good place to put it would be in auth.pl. Just after
open(AUTH, ">$auth_dir/$db_uid") or &cgierr("unable to open auth file: $auth_dir/$uid. Reason: $!\n");
print AUTH "$uid: $ENV{'REMOTE_HOST'}\n";
close AUTH;
add
&auto_delete;
And just so things are clear, I'll post the entire auto_delete subroutine. It can go anywhere in db.cgi -- you could just paste it onto the end of the script.
Code:
sub auto_delete {
$days = 30;
$time = time() - ($days * 86400);
my ($sec, $min, $hour, $day, $mon, $year, $dweek, $dyear, $daylight) = localtime($time);
my (@months) = qw!Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec!;
($day < 10) and ($day = "0$day");
$year = $year + 1900;
$in{'Date-lt'} = "$day-$months[$mon]-$year";
open(FILE, "< $db_file_name") or &cgierr("can't open $db_file_name: $!");
$count++ while <FILE>;
close FILE;
$in{'mh'} = $count;
my ($status,@hits) = &query("mod");
for (0 .. $db_total_hits - 1) {
%tmp = &array_to_hash($_, @hits);
$in{$tmp{$db_key}} = "delete";
}
my ($key, %delete_list, $rec_to_delete, @lines, $line, @data, $errstr, $succstr, $output, $restricted);
foreach $key (keys %in) {
if ($in{$key} eq "delete") {
$delete_list{$key} = 1;
}
}
open (DB, "<$db_file_name") or &cgierr("error in delete_records.
unable to open db file: $db_file_name.\nReason: $!");
if ($db_use_flock) { flock(DB, 1); }
@lines = <DB>;
close DB;
LINE: foreach $line (@lines) {
if ($line =~ /^$/) { next LINE; }
if ($line =~ /^#/) { $output .= $line; next LINE; }
chomp ($line);
@data = &split_decode($line);
$delete_list{$data[$db_key_pos]} ? ($delete_list{$data[$db_key_pos]} = 0) :
($output .= $line . "\n");
}
open (DB, ">$db_file_name") or &cgierr("error in delete_records. unable to open db file:
db_file_name.\nReason: $!");
if ($db_use_flock) {
flock(DB, 2) or &cgierr("unable to get exclusive lock on $db_file_name.\nReason: $!");
}
print DB $output;
close DB;
}
Notice that I took out the line
&html_home;
That's because, if you run the subroutine from auth.pl, it will take you to html_home anyway.
One thing to remember. I have not tested this, except that I'm pretty sure there aren't any syntax errors. Make up some data to test it on first, or at least be sure you back up your database to be sure it will do what you want it to do.
------------------
JPD