2015-03-03 70 views
0

routes.php文件驗證::嘗試不檢查用戶名和密碼

//form login 
Route::get('/', array('before' => 'guest', function() 
{ 
    return View::make('login'); 
})); 

// check login 
Route::post('login', '[email protected]'); 

HomeController.php

class HomeController extends BaseController { 

    /* 
    |-------------------------------------------------------------------------- 
    | Default Home Controller 
    |-------------------------------------------------------------------------- 
    | 
    | You may wish to use controllers instead of, or in addition to, Closure 
    | based routes. That's great! Here is an example controller method to 
    | get you started. To route to this controller, just add the route: 
    | 
    | Route::get('/', '[email protected]'); 
    | 
    */ 

    public function validate() 
    { 
     // set the remember me cookie if the user check the box 
     $remember = (Input::has('remember')) ? true : false; 
     // attempt to do the login 

     $email = Input::get('username'); 
     $pass = Input::get('password'); 
     $password = Hash::check($remember,$pass); 
     $credentials = array(
     'username' => "'$email'", 
     'password' => "'$pass'" 
     ); 
     $auth=Auth::attempt(array(
     'username' => "'$email'", 
     'password' => "'$pass'", 
)); 
     if($auth) { 
      return 'success'; 
     } 
     else { 
      return 'auth failed'; 
     } 

    } 

} 

user.php的

<?php 

use Illuminate\Auth\UserInterface; 
use Illuminate\Auth\Reminders\RemindableInterface; 

class User extends Eloquent implements UserInterface, RemindableInterface { 

    /** 
    * The database table used by the model. 
    * 
    * @var string 
    */ 
    protected $table = 'users'; 

    /** 
    * The attributes excluded from the model's JSON form. 
    * 
    * @var array 
    */ 
    protected $hidden = array('password'); 

    /** 
    * Get the unique identifier for the user. 
    * 
    * @return mixed 
    */ 
    public function getAuthIdentifier() 
    { 
     return $this->getKey(); 
    } 

    /** 
    * Get the password for the user. 
    * 
    * @return string 
    */ 
    public function getAuthPassword() 
    { 
     return $this->password; 
    } 

    /** 
    * Get the e-mail address where password reminders are sent. 
    * 
    * @return string 
    */ 
    public function getReminderEmail() 
    { 
     return $this->email; 
    } 

    public function getRememberToken() 
    { 
     return $this->remember_token; 
    } 

    public function setRememberToken($value) 
    { 
     $this->remember_token = $value; 
    } 

    public function getRememberTokenName() 
    { 
     return 'remember_token'; 
    } 

} 

的login.php

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml" lang="en"> 
    <head> 
     <title>Simple Login</title> 
    </head> 
<body> 
    <?php echo Form::open(array('url' => 'login', 'role' => 'form')) ?> 
     <h2>Please sign in</h2> 
     <!-- if there are login errors, show them here --> 
     <p> 
      <?php if(Session::has('flash_notice')): ?> 
       <div id="flash_notice"><?php echo Session::get('flash_notice') ?></div> 
      <?php endif; ?> 
     </p> 
     <input type="text" name="username" value="<?php echo Input::old('username') ?>" required="required" autofocus="autofocus" autocomplete="off" placeholder="Username" /> 
     <input name="password" placeholder="Password" required="required" type="password" /> 
     <div> 
      <input name="remember" type="checkbox" /> Remember me 
     </div> 
     <input type="submit" value="Sign in" /> 
    <?php echo Form::close() ?> 
</body> 
</html> 

我在我的xamppp機器上使用laravel4。我做了一個簡單的登錄表單,其中註冊用戶可以登錄,看到儀表板,但當我使用驗證::嘗試檢查用戶是否有效它總是會顯示一個錯誤'密碼的用戶名是錯誤的「

INSERT INTO `users` (`id`, `name`, `username`, `email`, `password`, `remember_token`, `created_at`, `updated_at`) VALUES 
(3, 'Imron Rosdiana', 'imron02', '[email protected]', '$2y$10$bAwhqg41gHPOLD36aVxQi.ItviwlN663gCIt6S8H2VjeR8kZWHAZy', 'pvusxXobB9wAWKReXte5pv51vD4BKfPi4LjEV9JUlArLdmc4DL30eipAJ6Nb', '2014-04-22 16:45:57', '2014-04-28 03:45:26'); 

用戶名:imron02 密碼:123456(通過使用哈希::讓我插入了密碼)

我知道這麼多的用戶已經問過這個問題,但我仍陷在每當我得到驗證失敗

回答

0

更改這一部分:

$credentials = array(
     'username' => "'$email'", 
     'password' => "'$pass'" 
     ); 
$auth=Auth::attempt(array(
     'username' => "'$email'", 
     'password' => "'$pass'", 
)); 

要這樣:

$credentials = array(
     'username' => $email, 
     'password' => $pass 
     ); 
$auth=Auth::attempt($credentials); 

參考this

雖然這樣做確保你有一個對應於你的表中的用戶名的列。我不得不提這個,因爲你提供的用戶名似乎不是電子郵件ID。用戶名和密碼組合應存在於相應的表中以便進行身份驗證。

如果您正計劃採取電子郵件ID,因爲你需要改變這一行的用戶名:

'username' => $email, 

這樣:

'email' => $email, 
0

您需要從變量中刪除引號。 $ email和$通過

$auth=Auth::attempt(array 
     (
     'username' => $email, 
     'password' => $pass, 
    )