Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何修改";“记住我”;Laravel 5.2+;中的过期时间;?_Laravel - Fatal编程技术网

如何修改";“记住我”;Laravel 5.2+;中的过期时间;?

如何修改";“记住我”;Laravel 5.2+;中的过期时间;?,laravel,Laravel,使用php artisan make:auth后,Laravel的“记住我”将无限期地记住用户 如何改变时间?像让它在7天内过期吗?步骤1 在LoginController中,您将看到使用AuthenticatesUsers 让我们将受保护的函数sendLoginResponse(Request$Request)从authenticateUsers复制到LoginController 步骤2 我们可以在服务器响应浏览器之前更改cookie的过期时间。让我们在LoginController中的se

使用php artisan make:auth后,Laravel的“记住我”将无限期地记住用户

如何改变时间?像让它在7天内过期吗?

步骤1 在
LoginController
中,您将看到
使用AuthenticatesUsers

让我们将受保护的函数sendLoginResponse(Request$Request)从
authenticateUsers
复制到
LoginController

步骤2 我们可以在服务器响应浏览器之前更改cookie的过期时间。让我们在
LoginController
中的
sendLoginResponse()
中添加一些代码。像这样

class LoginController extends Controller
{
    ...

    protected function sendLoginResponse(Request $request)
    {
        // set remember me expire time
        $rememberTokenExpireMinutes = 60;

        // first we need to get the "remember me" cookie's key, this key is generate by laravel randomly
        // it looks like: remember_web_59ba36addc2b2f9401580f014c7f58ea4e30989d
        $rememberTokenName = Auth::getRecallerName();

        // reset that cookie's expire time
        Cookie::queue($rememberTokenName, Cookie::get($rememberTokenName), $rememberTokenExpireMinutes);


        // the code below is just copy from AuthenticatesUsers
        $request->session()->regenerate();

        $this->clearLoginAttempts($request);

        return $this->authenticated($request, $this->guard()->user())
            ?: redirect()->intended($this->redirectPath());
    }
}

Use@Hexor有问题,当用户首次登录时,您不能使用 Cookie::get($rememberTokenName); 它是空的

您应该首先获取cookie队列值,然后重置cookie过期时间

$rememberTokenExpireMinutes = 20;

// 首先获取 记住我 这个 Cookie 的名字, 这个名字一般是随机生成的,
// First, get remember me cookie name. This is randomly generated.
$rememberTokenName = \Auth::getRecallerName();

$cookieJar = $this->guard()->getCookieJar();

$cookieValue = $cookieJar->queued($rememberTokenName)->getValue();

$cookieJar->queue($rememberTokenName, $cookieValue, $rememberTokenExpireMinutes);

$jumpUrl = '/user/xxxx';

return $this->authenticated($request, $this->guard()->user())
    ?: redirect()->intended($jumpUrl);
在拉威尔5.8

class CookiesJar.php

function forever($name, $value, $path = null, $domain = null, $secure = null, $httpOnly = true, $raw = false, $sameSite = null)

return $this->make($name, $value, 2628000, $path, $domain, $secure, $httpOnly, $raw, $sameSite);
换成这个

return $this->make($name, $value, 1440, $path, $domain, $secure, $httpOnly, $raw, $sameSite);

我知道这个问题已经很老了,但我很难找到Laravel7.3的解决方案,所以我想我应该在这里添加对我有用的东西

在App\Http\Controllers\Auth\LoginController.php文件中,进行以下更改

//Add this after namespace declaration
use Illuminate\Http\Request;

//Add this function to the class
protected function sendLoginResponse(Request $request)
    {
        $rememberTokenExpiresAt = 60*24*30; //expires in 30 days
        $rememberTokenCookieKey = $this->guard()->getRecallerName();
        $cookieJar = $this->guard()->getCookieJar();

  /* check if remember me token exists and then override it using the same name and value but different expiration time.
 
If you don't add the if condition, it will throw an error when user doesn't check the remember me box*/
        if ($cookieJar->queued($rememberTokenCookieKey)) {
            $cookieValue = $cookieJar->queued($rememberTokenCookieKey)->getValue();
            $cookieJar->queue($rememberTokenCookieKey, $cookieValue, $rememberTokenExpiresAt);
        }

        $request->session()->regenerate();

        $this->clearLoginAttempts($request);

        if ($response = $this->authenticated($request, $this->guard()->user())) {
            return $response;
        }

        return $request->wantsJson()
                    ? new JsonResponse([], 204)
                    : redirect()->intended($this->redirectPath());
    }

通过为
使用AuthenticatesUsers
指定别名,然后调用该实现,可以避免粘贴AuthenticatesUsers的代码。例如,
使用authenticateUsers{sendLoginResponse as baseSendLoginResponse;}
,然后在您的sendLoginResponse实现中,您可以使用
return baseSendLoginResponse($request)完成它当我使用此代码时,它会创建具有正确过期时间的cookie,但是如果我删除主laravel会话cookie,则记住cookie不会让用户登录。由于某种原因,它强制用户重新登录。由于编辑供应商文件是一个非常糟糕的主意,所以被否决。假设您将代码推送到远程存储库,您的供应商目录通常会被忽略,您的团队或同事将永远无法获得您的编辑。或者假设您更新了一个包,文件将被重写,您所做的更改将丢失。说真的,不要这样做。@louisfischer只要你能自动进行修补,这就不是问题。我们已经在一个laravel项目中使用了几年的供应商补丁,而且效果很好。当然,您需要将其添加到编写器“post update cmd”和“post install cmd”中。这应该是正确的答案。删除主会话cookie时,“选择答案”不起作用,出于某种原因,它不会使用“记住我”cookie。然而,这一点是正确的。我的猜测是,它获取cookie值的方式是关键。