我必須限制登錄,如果大規模失敗嘗試進入,我想阻止所有IP。如何使用以下代碼實現該目標?如果下面的代碼不夠好,請告知關於此事的好教程。登錄限制並阻止所有IP地址
<?php
$throttle = array(1 => 1, 10 => 2, 1000 => 'captcha');
$getfailedq = 'SELECT MAX(attempted) AS attempted FROM failed_logins';
$getfailed = $muc->prepare($getfailedq);
$getfailed->execute();
if ($getfailed->rowCount() > 0) {
$row = $getfailed->fetch(PDO::FETCH_ASSOC);
$latest_attempt = (int) date('U', strtotime($row['attempted']));
$getfailedq = 'SELECT Count(*) AS failed FROM failed_logins WHERE attempted > Date_sub(Now(), INTERVAL 15 minute)';
$getfailed = $muc->prepare($getfailedq);
$getfailed->execute();
if ($getfailed->rowCount() > 0) {
$row = $getfailed->fetch(PDO::FETCH_ASSOC);
$failed_attempts = (int) $row['failed'];
krsort($throttle);
foreach ($throttle as $attempts => $delay) {
if ($failed_attempts > $attempts) {
if (is_numeric($delay)) {
$remaining_delay = time() - $latest_attempt + $delay;
echo 'You must wait ' . $remaining_delay . ' seconds before your next login attempt';
} else {
echo "captcha";
}
break;
}
}
}
}
?>
爲什麼你綁定參數,當你的查詢都沒有任何參數在他們的? –
這是一個很好的問題,謝謝,修正了查詢 – Serjio
'if($ getfailed-> rowCount()> 0){'這將永遠是真實的,所以你的第一個查詢是毫無意義的 – cmorrissey