Session variables have a flaw in that they just have to deny cookies and then they are exempt.
You would be better off creating a table for this in a database with the userid.
create table failedlogins (
id INT NOT NULL auto_increment,
user_id INT NOT NULL,
time_tried DATETIME NOT NULL,
primary key(id),
index(user_id));
Then you just insert a record on a failed attempt. Then, when the user attempts to login you check "loginLocked($user);" which would query that table for the current time - x minutes (where x minutes is the time you want spaced through x attempts) So say 3 attempts allowed per 5 minutes:
function loginLocked($user) {
$query = "SELECT count(fl.id) FROM failedlogins fl
JOIN user u ON fl.user_id = u.id
WHERE time_tried < DATE_SUB(NOW(), INTERVAL 5 MINUTE) AND u.username = '" . mysql_real_escape_string($user) . "'";
$res = mysql_query($query) or trigger_error("Failed Login Query failed: " . mysql_error();
$attempts = mysql_result($res, 0, 0);
return ($res < 3);
}
That assumes the tables etc. But should give you a good start / idea on how to handle it. You will have to insert an entry each time a login fails.
EDIT:
My SQL Time check may be wrong, as I do not have this setup and I wrote it on the spot, that might be an area that needs adjusting.
Added the user to the where clause, sorry forgot about that.
Clarification: Yea, Locked should be more or less a (re)Captcha is now required to access the account on it's next login attempt.