怎么做"记住我" laravel中的登录选项?

时间:2014-04-24 19:46:52

标签: php authentication laravel laravel-4

所以我正在研究的是当我检查记住我复选框时,用户在注销表单(用户名和密码)时记住用户数据,如:enter image description here

所以这是我的代码不起作用:(

// create our user data for the authentication
        $userdata = array(
                'email'     => Input::get('email'),
                'password'  => Input::get('password')
        );

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

            Session::flash('message', array('body'=>trans('login-signup.welcome'), 'type'=>'success'));
            return Redirect::to('/');

    }

4 个答案:

答案 0 :(得分:4)

使用Cookie

  

cookie,是用户浏览该网站时从网站发送并存储在用户的网络浏览器中的一小段数据。每次用户加载网站时,浏览器都会将cookie发送回服务器,以通知网站用户之前的活动

创建:

$response->withCookie(Cookie::make('name', 'value', $minutes));

要检索

$value = Cookie::get('name');

您的问题是不记得用户登录..问题是如何根据保存的身份验证信息填充输入。如果在加载页面时在输入值属性中打印身份验证值,则可以执行此操作。

larval Cookies Docs

Laravel也有自己的实施"记住我"

if (Auth::attempt(array('email' => $email, 'password' => $password), true))
{
// The user is being remembered...
}

if (Auth::viaRemember())
{
//
}

有关"Authenticating A User And "Remembering" Them"

的更多信息

答案 1 :(得分:2)

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
    $auth = Auth::attempt(
        [
            'username'  => strtolower(Input::get('username')),
            'password'  => Input::get('password')    
        ], $remember
    );
    if ($ auth) {
        return Redirect::to('home');
    } else {
        // validation not successful, send back to form 
        return Redirect::to('/')
            ->with Input(Input::except('password'))
            ->with('flash_notice', 'Your username/password combination was incorrect.');
    }

}

答案 2 :(得分:1)

$remember = true;
Auth::login($user, $remember);

答案 3 :(得分:0)

您可以通过设置 Cookie 来记住登录信息。 Cookie将存储在浏览器存储中。它是网站发送的一小段数据,并在用户浏览特定网站时存储在用户的Web浏览器中。每次用户加载网站时,浏览器都会将cookie发送回服务器,以通知网站用户的先前活动。在Laravel中,您可以将Cookie设置为

$response->withCookie(Cookie::make('COOKIE_NAME', 'COOKIE_VALUE', $minutes));
//or
back()->->withCookie(Cookie::make('COOKIE_NAME', 'COOKIE_VALUE', $minutes))

如果您要从中间件设置cookie,请按以下方式操作

public function handle($request, Closure $next){
    $response = $next($request);
    $response->withCookie(Cookie::make('COOKIE_NAME', 'COOKIE_VALUE', $minutes))
}

您可以通过 Illuminate\Support\Facades\Cookie 类创建/检索cookie,也可以使用laravel的全局 cookie() 辅助函数。

要获取Cookie,

$value = Cookie::get('COOKIE_NAME');
//or
$request->cookie('COOKIE_NAME')

  

请记住,Laravel默认情况下会加密cookie。中间件 EncryptCookies 负责加密cookie。检索此类Cookie会得到null。禁用/删除EncryptCookies中间件不是解决方案。

您可以按照以下方式对cookie进行加密,

protected $except = [
    'COOKIE_NAME',
];