Php 填写表单的Laravel检查会话超时
在我的登录表单中,我想为填写表单设置检查会话超时,填写表单后,我想检查长时间,如果会话过期表单必须由用户重新创建以填写,我使用此方法,但我得到错误: 我的代码:Php 填写表单的Laravel检查会话超时,php,laravel,Php,Laravel,在我的登录表单中,我想为填写表单设置检查会话超时,填写表单后,我想检查长时间,如果会话过期表单必须由用户重新创建以填写,我使用此方法,但我得到错误: 我的代码: public function checkAccount(CheckAuthenticationRequest $request) { if ((time() - Session::activity()) > (Config::get('session.lifetime') * 60)) { re
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\u user\u func\u array()要求参数1为有效回调,
类“Illumb\Session\Store”没有方法“activity”
错误:如果我的表单不再由用户填写并尝试发送表单数据
TokenMismatchException
该错误表示会话没有您正在调用的方法。尝试使用
Session::get('lastActivityTime')
通过如下方式设置会话来实现
lastActivityTime
:session::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
}
}
类formController扩展控制器{
/**返回表单视图的函数**/
公共功能展示表单(请求$Request){
$request->session()->put(“opendate”,Carbon::now());//在会话中放置当前时间戳
返回视图(“myform”);
}
/**处理表单数据的函数**/
公共功能公共功能检查帐户(请求$Request){
如果(!$request->session()->has(“opendate”))返回中止(400);
$open_date=$request->session()->pull(“opendate”);
//检查会话中的数据与现在的数据之间的差异
如果($opendate->diffInMinutes(Carbon::now())>10)//back()
->withErrors(“登录表单不再填写,您必须重新填写”)
->withInput();
//这里是成功代码
}
}
您可能想解释一下lastActivityTime
会话密钥的来源,因为Laravel没有这样的东西。您应该在每个请求之后自己实现它:session::set('lastActivityTime',time())
然后请在回答中包含它,以便人们知道您指的是什么,不必猜测。你确定你在顶部导入了正确的类吗?Laravel的会话存储没有activity
方法,而且你无法可靠地确定会话何时过期,因为Laravel可能已经用新会话替换了旧会话。因此,处理这个问题的一种实用方法是添加一个会话键,我们称之为is_active
,然后检查该键是否存在if(session::has('is_active')){…}
。如果处于活动状态
键仍在,则表示会话未过期;如果缺少该键,则表示会话已过期并删除了该键(您需要将其放回)。@JoelHinz是的,先生。我写了使用Session代码>我的class@Bogdan不幸的是,如果我的表格不再由用户填写。我得到了TokenMismatchException
错误,我必须处理它。@JoelHinz是对的,activity
是从Laravel 3:)回来的。我已经正确地标记了这个问题,这样其他人就不会发生这种情况。