Auth :: attempt($ credentials)总是返回false

时间:2012-11-05 11:30:25

标签: php laravel

您好,我是laravel的新手,我正在尝试编写登录表单的功能,这里是代码:

这就是我创建新用户的方式(效果很好)

public function action_newuser()
{

    $email = Input::get('email');
    $password = Input::get('password');
    $new_user = Input::get('new_user', 'off');
    $first_name= Input::get('firstname');
    $last_name= Input::get('last_name');
    $username= Input::get('username');

    $user = new User();
    $user->email = $email;
    $user->password = Hash::make($password);
    $user->first_name =$first_name;
    $user->last_name= $last_name;
    $user->username= $username;
    try{

        $user->save();
    }
    catch(Exception $e) {
        echo "Failed to create new user!";
    }

这是我的登录功能:

public function action_login()
{   

    $password = Input::get('password');    
    $username= Input::get('username');
    $remember= Input::get('remember', 'off');
    if($remember =='off')   $remember=FALSE;
    if($remember =='on') $remember=TRUE;
    $credentials = array(
            'username' => $username,
            'password' => $password,
            'remember' => $remember   
        );
     if( Auth::attempt($credentials)) 
        {
            return Redirect::to('dashboard/index');
        } 
        else 
        {
            #I put this line to check are the values passed from my form are correct.
            echo $password.'  ' .$username.' '. $remember;

        }


}   

当我提交登录表单时,它总是显示空白页面的值为$ password,$ username和$ remember值。这意味着Auth :: attempt()运行不正常。

可能是什么问题?

3 个答案:

答案 0 :(得分:3)

检查以确保您的数据库密码字段为60个字符。

答案 1 :(得分:3)

欺骗我! 虽然我已多次检查过,但我在auth配置文件中看不到行'username' => 'email',

它应该是'username' => 'username',,因为我将使用用户名进行登录。

答案 2 :(得分:0)

对于所有使用有效信用卡面临Auth :: attempt()登录失败的laravel 4开发人员!

解决方案:

  • 在app / config / app.php中更改类型:' cipher' => MCRYPT_RIJNDAEL_128到' cipher' => MCRYPT_RIJNDAEL_256并重新哈希所有密码
  • 在模型添加方法中:

     public function setPasswordAttribute($pass) {
    
         $this->attributes['password'] = Hash::make($pass);
     }
    
  • 在UserController中:

    //创建用户或注册

       public function postCreate() {
    
        $input = Input::all();
        $user = new User;
        $user->username = Input::get('username');
        $user->email = Input::get('email');
        $user->password = Hash::make(Input::get('password'));    
        $user = User::create($input);
        return Redirect::action("Frontend\HomeController@index");
    

    }

    //登录或登录

    public function postSignin() {
    
        $userdata = array(
            'username' => Input::get('username'),
            'password' => Input::get('password')
        );
    
        if (Auth::attempt($userdata, true)) {
             Session::put('username', Auth::user()->username);
             return Redirect::action("Frontend\HomeController@index");
        } else {
             return Redirect::action("Frontend\UserController@getLogin")
                    ->with('message', 'Your username/password combination was incorrect')
                    ->withInput();
    }
    

希望我帮助了那里的人