You can use the timestamp fieldtype in mysql - just make sure that some other fields are updated, then this will automatically be updated.
I made a thing like this once, not for db but for files.
The gist of it is:
- one script with either auth content or login screen, depending on successful login
- before any auth exec (including login check for user/pass), open a text-file and write the count of login-attempts to it
## Check for erred logins - more than 3 (number more than 6 in $thisip.cnt-file), and user gets timer ##
$chk_hack = @file_get_contents("$countlogindir/$thisip^.cnt");
$chk_hack = trim($chk_hack);
## If more than 5 login-tries, include timeout ##
$time_out = time() - 300;
if($chk_hack > 6 && filemtime("$countlogindir/$thisip^.cnt") > $timeout) {
@touch("$countlogindir/$thisip^.cnt");
include("time_out.php");
exit;
} else {
if($rm_cnt == "y") {
## Check if the user tries to post to this state conciously ##
if($chk_hack <= 6) {
@touch("$countlogindir/$thisip^.cnt");
header("Location:login.php");
## Finally - if the timeout truly is endured: delete count and cleared for login ##
} else {
@unlink("$countlogindir/$thisip^.cnt");
header("Location:login.php");
}
}
The key of which to check for hack is the user-agent string ($thisip).
This code is placed on top of the script, before any significant happens - thus it exits if the user has to many.
Further down, I placed some code (in the login-switch) to write the counts to the file.
## Check for count file - and init count if not found ##
if (!file_exists("$countlogindir/$thisip^.cnt")) {
@touch("$countlogindir/$thisip^.cnt");
$cnt = "";
$cnt = "1";
$in_touch = fopen("$countlogindir/$thisip^.cnt", "w");
fputs($in_touch, $cnt);
fclose($in_touch);
## Count file present - set count = count + 1 ##
} else {
$instring = "";
$instring = file_get_contents("$countlogindir/$thisip^.cnt");
$instring = trim($instring);
$nstring = $instring + 1;
$in_upd = fopen("$countlogindir/$thisip^.cnt", "w");
fputs($in_upd, $nstring);
fclose($in_upd);
}
The script time_out.php is just a page with pain html, and a javascript counter to count down to login. Once counted down - the page is reloaded and the login-screen appears.
<script language=\"JavaScript\">
//configure refresh interval (in seconds)
var countDownInterval=180;
//configure width of displayed text, in px (applicable only in NS4)
var c_reloadwidth=200
</script>
<ilayer id=\"c_reload\" width=&{c_reloadwidth}; ><layer id=\"c_reload2\" width=&{c_reloadwidth}; left=0 top=0></layer></ilayer>
<script>
var countDownTime=countDownInterval+1;
function countDown(){
countDownTime--;
if (countDownTime <=0){
countDownTime=countDownInterval;
clearTimeout(counter)
window.location.replace('publicator.php?rm_cnt=y')
return
}
if (document.all) //if IE 4+
document.all.countDownText.innerText = countDownTime+\" \";
else if (document.getElementById) //else if NS6+
document.getElementById(\"countDownText\").innerHTML=countDownTime+\" \"
else if (document.layers){
document.c_reload.document.c_reload2.document.write('Timeout:<br><b id=\"countDownText\">'+countDownTime+' </b> seconds')
document.c_reload.document.c_reload2.document.close()
}
counter=setTimeout(\"countDown()\", 1000);
}
function startit(){
if (document.all||document.getElementById)
document.write('Timeout:<br><b id=\"countDownText\">'+countDownTime+' </b> seconds')
countDown()
}
if (document.all||document.getElementById)
startit()
else
window.onload=startit
</script>
Backslashed code! 😃
Never been tested against the real beeg bad guys, though.
knutm :-)