填写表单的Laravel检查会话超时


Laravel check session timeout for fill form

在我的登录表单中,我想为填写表单设置检查会话超时,填写表单后,我想检查很长时间,如果会话过期的表单必须重新创建才能由用户填写,我使用这种方法,但我得到错误:

我的代码:

public function checkAccount(CheckAuthenticationRequest $request)
{
    if ((time() - Session::activity()) > (Config::get('session.lifetime') * 60))
    {
        return redirect()->back()
            ->withErrors('Login form no longer fill, you must be fill again')
            ->withInput();
    }
    ...
}

错误:用于以上代码

call_user_func_array()期望参数1是有效的回调,类"Illuminate''Session''Store"没有方法"activity"

错误:如果我的表单不再由用户填写并尝试发送表单数据

TokenMismatchException

错误表明Session没有您正在调用的方法。尝试使用Session::get('lastActivityTime')

通过如下设置会话来实现lastActivityTimeSession::set('lastActivityTime', time())

您可以这样做。

class formController extends Controller{
    /**FUNCTION THAT RETURNS THE FORM VIEW**/
    public function showForm(Request $request) {
        $request->session()->put("opendate", Carbon::now()); //put current timestamp in the session
        return view("myform");
    }
    /**FUNCTION THAT HANDLES THE FORM DATA**/
    public function public function checkAccount(Request $request){
        if(!$request->session()->has("opendate")) return abort(400);
        $open_date = $request->session()->pull("opendate");
        //check the difference between data in the session and now
        if($opendate->diffInMinutes(Carbon::now()) > 10) //<-- expire minutes here) 
            return redirect()->back()
                            ->withErrors("Login form no longer fill, you must be fill again")
                            ->withInput();
        //success code here
    }
}