2016-11-01 65 views
0

我試圖在laravel 4.2的一個項目上工作,但現在,我想限制用戶的登錄嘗試次數。幾乎所有的解決方案都是laravel 5並且不熟悉與5.所以任​​何人有一個想法,請我需要你的幫助。謝謝。如何限制laravel 4用戶的登錄嘗試

$data = Input::all(); 
    $rules = array(
     'username' =>'required|', 
     'password' => 'required|min:6' 
    ); 
    $validator = Validator::make($data, $rules); 
    if($validator->passes()) 
    {    
     $user = array(
      'username'=> Input::get('username'), 
      'password' => Input::get('password'), 
      'active'=>1,       
     ); 

     if(Auth::attempt($user)) 
     { 
       if((Auth::user()->isBuyer())) 
       { 
        return Redirect::route('customer')->with(['success' => 'Welcome <strong style="color:black">'.Auth::user()->name .'</strong>']); 
       } 
       elseif((Auth::user()->isSeller())) 
       { 
        return Redirect::route('seller')->with(['success' => 'Welcome <strong style="color:black">'.Auth::user()->name .'</strong>']); 

       } 
       else 
       { 
        return Redirect::route('dashboard')->with(['success' => 'Welcome <strong style="color:black">'.Auth::user()->name.' </strong>']); 

       }  
     } 
     return Redirect::back() 
      ->with(array(
       'error' => 'Your email and password combination is invalid or Verify your email first' 
      )); 

    } 
    return Redirect::back() 
     ->withInput() 
     ->withErrors($validator); 

} 

是在

+0

您可以爲您的用戶表添加一個'attempt'列。每增加一次,用戶嘗試登錄並檢查總嘗試次數 – Wistar

回答

0

用於記錄我的控制器功能有這麼多的方式來做到這一點。但下面是常見的,我通常會這樣做。

1.)將列「嘗試」添加到用戶表中並在用戶每次登錄失敗時增加。如果用戶成功登錄,則將列重置爲0.

2.)創建會話「嘗試」並在用戶每次登錄失敗時增加。如果用戶成功登錄,則將會話重置爲0.您還可以阻止用戶登錄x分鐘。

但我更喜歡選項1,所以即使用戶使用不同的瀏覽器,用戶也無法登錄。

+0

非常感謝您 – Tongi

+0

您的歡迎,我希望它有幫助。 –

+0

我試過選項1,它完美的工作,謝謝 – Tongi