File manager - Edit - /home/webapp69.cm.in.th/u69319090028/Shop/app/Models/LoginAttempt.php
Back
<?php /** * LoginAttempt Model * Tracks failed login attempts and brute-force lockouts */ require_once __DIR__ . '/../Core/Model.php'; class LoginAttempt extends Model { protected string $table = 'login_attempts'; public function checkLockout(string $identifier, string $ip): array { $sql = "SELECT * FROM `login_attempts` WHERE `identifier` = :identifier AND `ip_address` = :ip LIMIT 1"; $attempt = $this->fetchOne($sql, [':identifier' => $identifier, ':ip' => $ip]); if ($attempt && !empty($attempt['locked_until'])) { $lockedUntil = strtotime($attempt['locked_until']); $now = time(); if ($lockedUntil > $now) { $remainingMinutes = ceil(($lockedUntil - $now) / 60); return [true, $remainingMinutes]; } } return [false, 0]; } public function recordFailure(string $identifier, string $ip, int $maxAttempts = 5, int $lockoutSeconds = 900): bool { $sql = "SELECT * FROM `login_attempts` WHERE `identifier` = :identifier AND `ip_address` = :ip LIMIT 1"; $attempt = $this->fetchOne($sql, [':identifier' => $identifier, ':ip' => $ip]); if ($attempt) { $newAttempts = (int)$attempt['attempts'] + 1; $lockedUntil = null; if ($newAttempts >= $maxAttempts) { $lockedUntil = date('Y-m-d H:i:s', time() + $lockoutSeconds); } $updateSql = "UPDATE `login_attempts` SET `attempts` = :attempts, `locked_until` = :locked_until, `last_attempt_at` = NOW() WHERE `id` = :id"; $this->query($updateSql, [ ':attempts' => $newAttempts, ':locked_until' => $lockedUntil, ':id' => $attempt['id'] ]); return $newAttempts >= $maxAttempts; } else { $this->insert([ 'identifier' => $identifier, 'ip_address' => $ip, 'attempts' => 1, 'locked_until' => null, 'last_attempt_at' => date('Y-m-d H:i:s') ]); return false; } } public function clearAttempts(string $identifier, string $ip): void { $sql = "DELETE FROM `login_attempts` WHERE `identifier` = :identifier AND `ip_address` = :ip"; $this->query($sql, [':identifier' => $identifier, ':ip' => $ip]); } }
| ver. 1.4 |
Github
|
.
| PHP 7.4.33 | Generation time: 0.24 |
proxy
|
phpinfo
|
Settings