[英]limited attempts for login and block a users account for a specific amout of time
美好的一天,我在創建登錄嘗試時遇到問題,如果用戶輸入了5次嘗試的錯誤詳細信息,則會阻止現有帳戶登錄,我有一個Ajax和一個數據庫
我嘗試捕獲值為0的嘗試。然后每次失敗都給出+ 1,但不會增加超過1的次數,因為我認為ajax命令會再次刷新它,因此嘗試僅接收1次循環。
登錄部分正在工作,但我無法進行嘗試,並且您有任何想法如何在嘗試次數大於5的情況下(阻止用戶15分鍾)阻止用戶。我已經為其搜索了代碼,但該代碼最后一次使用是2007年,因此該代碼不能被PHP讀取
這是我的ajax代碼:
在登錄頁面上,這是ajax代碼:
> <script type="text/javascript"> function validLogin(){
> var uname=$('#uname').val();
> var password=$('#password').val(); var attempts=0;
>
> var dataString = 'uname='+ uname + '&password='+ password;
> $("#flash").show();
> $("#flash").fadeIn(400).html('<img src="images/loading.gif" />');
> $.ajax({
> type: "POST",
> url: "login_processed.php",
> data: dataString,
> cache: false,
> success: function(result){
> var result=trim(result);
> $("#flash").hide();
> if(result=='correct0'){
> window.location='user_home.php';
> }
> else if(result=='correct1'){
> window.location='admin_home.php';
> }else{
> $("#errorMessage").html(result);
> $attempts++;
> }
>
> }
> }); } function trim(str){
> var str=str.replace(/^\s+|\s+$/,'');
> return str; } </script>
這是用於處理登錄信息的login_process.php,它捕獲結果和錯誤。
> $message=array(); $attempts=0; if(isset($_POST['uname']) &&
> !empty($_POST['uname'])){
> $uname=mysql_real_escape_string($_POST['uname']); }else{
> $message[]='Please enter username'; }
>
> if(isset($_POST['password']) && !empty($_POST['password'])){
> $password=mysql_real_escape_string($_POST['password']); }else{
> $message[]='Please enter password'; }
>
> $countError=count($message);
>
> if($countError > 0){
> $attempts++; echo $attempts; for($i=0;$i<$countError;$i++){
> echo ucwords($message[$i]).'</br>';
>
> }
>
> }
>
>
> else{
> $query="select * from user where uname='$uname' and BINARY password='$password'";
>
> $res=mysql_query($query);
> $checkUser=mysql_num_rows($res); $row = mysql_fetch_assoc($res);
> if($checkUser > 0){
> $_SESSION['LOGIN_STATUS']=true;
> $_SESSION['UNAME']=$uname;
> echo 'correct'.$row['type'];
> }else{
> echo ucwords('Incorrect Username or Password');
>
> } }
任何幫助將非常感激。 並且您可以教我阻止用戶登錄的嘗試,因為如果我阻止嘗試錯誤的用戶的ip,它可能會廣泛影響一個人登錄的地方,例如計算機商店。 :D謝謝
1.您應該創建一個數據庫數據庫表,稱為“嘗試”或類似名稱。 然后將您已經存在的用戶表中的用戶ID放在其中。
然后,每次有人嘗試登錄時,代碼都會檢索當前嘗試的次數並將其添加1,然后更新到表中。
然后寫一條語句,如果單個帳戶的嘗試次數大於5,則會顯示一條消息,告訴用戶他們無法登錄。
使用會話變量來設置超時和鎖定功能。
像這樣
session_start();
$attempts = "your MySQL result from the attempts table";
if($attempts < 5){
//normal login with error message
$next_attempts = $attempts++;
//code to write $new_attempts to attempts table
//set the start of the lockout timer
if($attempts == 4){
$_SESSION('timeout') = time();
}
}
//LOCKED OUT CODE
else{
echo "sorry you have to wait 15 min to log in again";
//Check elapsed time
//10 minute timeout
if ($_SESSION['timeout'] + 10 * 60 < time()) {
$attempts = 0;
//code to write $attempts to table attempts
session_destroy()
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.