Session is expiring automatically in laravel 5.2

喜夏-厌秋 提交于 2020-01-06 04:59:08

问题


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:

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

}

My login form code,

<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>

回答1:


I got the solution but it seems strange for me. In config/session.php
i changed this
'cookie' => 'laravel_session',
to
'cookie' => 'myapp_session',
I got this suggestion from laracasts click link here
I still feeling strange about this, if anyone know the reasons behind the issue please let me know.




回答2:


Don't forget about Session::save() or session()->save()



来源:https://stackoverflow.com/questions/46216511/session-is-expiring-automatically-in-laravel-5-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!