rahul16590
rahul16590

Reputation: 401

Unable to store session in Database in laravel 5.2

I recently upgraded my application from laravel 5.1 to 5.2 version. Also I am trying to store session in Database which is currently stored in file.

I have changed .env file to include SESSION_DRIVER=database. Also I have changed session.config file to include 'driver' => env('SESSION_DRIVER', 'database') and generated required session table.

But when I tried to login to the application I am getting TokenMismatchException in VerifyCsrfToken.php line 67: If I change SESSION_DRIVER=file then application is working as expected.

But this was not the case in Laravel 5.1 version.

What is changed in Laravel 5.2 version and what steps are required to store user session in database?

Upvotes: 2

Views: 6165

Answers (2)

Andre F.
Andre F.

Reputation: 1163

I had to deal with this issue myself as well. So the initial steps are as you had already mentioned in your question.

  • On fresh install of laravel and verify that everything works, change the SESSION_DRIVER=file to SESSION_DRIVER=database

  • Run the following commands IN ORDER in your console within the directory of your app:

    php artisan session:table
    composer dump-autoload
    php artisan migrate
    
  • (This step might be different from what you did) In your config\session.php file search for:

    'driver' => env('SESSION_DRIVER', 'file'),
    

    and change it to the following

    'driver' => env('SESSION_DRIVER', 'app.database'),
    
  • Within the same config\session.php file change the encrypt configuration to true

  • Set your connection and table name settings appropriately

     'connection' => 'database_name_here',
     'table' => 'sessions', //here you can change but sessions is recommended until you get more comfortable with how it all works
    
  • Attempt to login to your application and check your database to see if it all works.

If more help is needed you can also check a few resources that have helped me solve the issue. Keep in mind these resources are of people having similar issues but maybe your problem is similar to theirs. Only way to find out is to investigate. Hope this helps! =) :

https://laracasts.com/discuss/channels/laravel/how-to-store-users-id-in-session-table-laravel-5

https://laravel.com/docs/5.2/session

(Video tutorial on Laravel Sessions, VERY good place to help you get to where you need to be!) https://www.youtube.com/watch?v=-FMecyZs5Cg

Upvotes: 4

Mohamed Elbiheiry
Mohamed Elbiheiry

Reputation: 353

look it's ok to go with it manual but why don't u use the php artisan make:auth it creates every thing u need for authentication and also helps u keeping the session instead of doing it manual ?

Upvotes: -1

Related Questions