duanmei1694 2014-08-24 12:22
浏览 9
已采纳

Laravel还记得我,用Auth :: try运行吗?

So I have this code:

public function postLogin() {
    // validate the info, create rules for the inputs
    $rules = array(
        'username'    => 'required', // make sure the email is an actual email
        'password' => 'required|alphaNum|min:3' // password can only be alphanumeric and has to be greater than 3 characters
    );

    // run the validation rules on the inputs from the form
    $validator = Validator::make(Input::all(), $rules);

    // if the validator fails, redirect back to the form
    if ($validator->fails()) {
        return Redirect::route('login')
            ->withErrors($validator) // send back all errors to the login form
            ->withInput(Input::except('password')); // send back the input (not the password) so that we can repopulate the form
    } else {

        $remember = (Input::has('remember')) ? true : false;
        // create our user data for the authentication
        $userdata = (array(
            'username'  => Input::get('username'),
            'password'  => Input::get('password')
        ), $remember);

        // attempt to do the login
        if (Auth::attempt($userdata)) {

            // validation successful!
            // redirect them to the secure section or whatever
            // return Redirect::to('secure');
            // for now we'll just echo success (even though echoing in a controller is bad)
            echo 'SUCCESS!';

        } else {

            // validation not successful, send back to form
            return Redirect::route('login')
             ->with('global', 'Incorrect username or password. Please try again.');
        }
    }
}

When it runs I get: syntax error, unexpected ','. Basically it isn't expecting the $remember to be passed there. Where is it meant to be? I've tried putting it here: Auth::attempt($userdate), $remember) { } but that didn't work either. It had the same error. Not sure what's going on. Any help would be appreciated.

  • 写回答

1条回答 默认 最新

  • dongzhong8834 2014-08-24 12:29
    关注

    You can use Auth::viaRemember() in your Authcontroller, to check if a user was already logged:

    if (Auth::check() || Auth::viaRemember()) {...
    

    And change your login-check as follows:

    //Assuming, the remember-input is a checkbox and its value is 'on'
    if (Auth::attempt($userData, (Input::get('remember') == 'on') ? true : false)) {...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化