Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

prudhvi259's avatar

Session is expiring automatically in laravel 5.2

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>
0 likes
6 replies
prudhvi259's avatar

@tisuchi already lifetime is 120 by default which means 2hrs but my session is expiring on refresh increasing the lifetime doesn't solve my issue.

tisuchi's avatar
tisuchi
Best Answer
Level 70

well, try this-

In your config/session.php

Change

'cookie' => 'laravel_session',

to

'cookie' => 'myapp_session',
4 likes
prudhvi259's avatar

@tisuchi it worked well and thanks a lot. feeling wonder about the issue. previously i worked on a project which was developed in same laravel 5.2 then i didn't get this issue. Can you explain about the reasons behind issue.

tisuchi's avatar

Nice to hear that. If it is working, make this answer as a correct answer.

Its because of the name of the cookie affects every driver.

3 likes

Please or to participate in this conversation.