会话在laravel 5.2中自动到期 [英] Session is expiring automatically in laravel 5.2

查看:132
本文介绍了会话在laravel 5.2中自动到期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序中使用了larvel 5.2,没有使用laravel身份验证,而是编写了自己的代码,成功登录时,我将用户名存储在会话变量中,并在登录后针对每个请求检查该用户名.我没有更改任何会话设置.我的问题是会话在不到3分钟的时间内过期,有时在登录时显示

I am using larvel 5.2 for my application, i didn't used laravel authentication and wrote my own code, at the time of successful login i am storing user name in a session variable and checking this for every request after login. I didn't changed any session settings. My problem is session is expiring in lessthan 3mins and sometimes while login it is showing

TokenMismatchException in VerifyCsrfToken.php line 67:

仅在某些时候记得.刷新页面后,所有内容均恢复正常,并且3分钟内登录后会话即将到期.我在路线中的代码如下,

remember only sometimes. After refresh the page everything become normal and after login within 3mins session is expiring. My code in routes is as follows,

Route::get('/', function () {
    if(Session::has('username') && Session::get('username') != ""){
        return view('index');
    }
    else{
        return redirect('login');
    }
});
Route::get('login', function () {
    if(Session::has('username') && Session::get('username') != ""){
        return redirect('/');
    }
    else{
        return view('login');
    }
});

我的登录名和会话存储为,

My login and session storing as,

public function checkme(Request $request){

    $this->validate($request, [
        'username' => 'required',
        'password' => 'required'
    ]);

    //return $request->all();
    $username = $request->username;
    $password = md5($request->password);

    $admin = Admin::where('username', $username)
                ->where('password', $password)
                ->where('status', 'active')
                ->first();

    if(is_null($admin)){

        Session::flash('Invalid','Invalid Credentials..!');
        return redirect('login');
    }
    else{

        Session::put('username',$admin->username);
        return redirect('/');
    }

}

我的登录表单代码,

<form class="login" method="post" action="login">
                    <input type="hidden" name="_token" value="<% csrf_token() %>">
                    <input type="text" placeholder="Username" name="username" required="true" />
                    <input type="password" name="password" placeholder="Password" required="true" />
                    <input type="submit" value="Login" class="btn btn-info btn-sm" />
                </form>

推荐答案

不要忘记Session::save()session()->save()

这篇关于会话在laravel 5.2中自动到期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆