php 如何在 Laravel 中执行“记住我”登录选项?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23278404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:37:15  来源:igfitidea点击:

how to do "remember me" login option in laravel?

phpauthenticationlaravellaravel-4

提问by Mohammadov

so what i am working on is when i check the remember mecheckbox the user when logout the form (username and password) remember the user data like that : enter image description here

所以我正在做的是,当我选中“记住我”复选框时,用户在注销表单(用户名和密码)时会记住这样的用户数据:在此处输入图片说明

so this is my code it not works :(

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

// 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('/');

    }

回答by Othman

Use Cookies

使用 Cookie

cookie, is a small piece of data sent from a website and stored in a user's web browser while the user is browsing that website. Every time the user loads the website, the browser sends the cookie back to the server to notify the website of the user's previous activity

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

To create:

创造:

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

To retrieve

检索

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

Your question is not to remember the user login.. The question is how to fill the inputs based on saved auth information. You can do that if you print the authentication values in the input value attribute while loading the page.

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

larval Cookies Docs

幼虫饼干文档

Also Laravel has it's own implementation of "Remember Me"

Laravel 也有它自己的“记住我”的实现

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

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

More information about "Authenticating A User And "Remembering" Them"

有关“对用户进行身份验证并“记住”他们的更多信息

回答by Rahul Tathod

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.');
    }

}

回答by Mike Thrussell

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

回答by Kiran Maniya

You can remember the login by setting Cookie. The cookie will be stored in browser storage. it is a small piece of data sent from a website and stored in a user's web browser while the user is browsing a particular website. Every time the user loads the website, the browser sends the cookie back to the server to notify the website of the user's previous activity. In Laravel, you can set the cookie as,

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

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

if you want to set cookie from middleware, here is the way

如果你想从中间件设置 cookie,这里是方法

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

You can either create/retrieve the cookie by Illuminate\Support\Facades\Cookieclass or you can use the laravel's global cookie()helper function.

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

To retrieve the cookie,

要检索 cookie,

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

Remember, Laravel encrypts cookie by default. The middleware EncryptCookiesis responsible to encrypting cookies. You will get nullon retrieval of such cookies. Disabling/Removing the EncryptCookiesmiddleware is not the solution.

请记住,Laravel 默认会加密 cookie。中间件EncryptCookies负责加密 cookie。您将开始null检索此类 cookie。禁用/删除EncryptCookies中间件不是解决方案。

you can except the cookie from being encrypted as follow,

您可以按照以下方式将 cookie 加密,

protected $except = [
    'COOKIE_NAME',
];