iqbal prabu
iqbal prabu

Reputation: 13

Token Miss Match When Session driver is Database Laravel

I have a problem when I change the driver session into database, always token miss match when I try login. I have done some actions like

php artisan config:clear
php artisan config:cache 
php artisankey:generate

and also change in config/session.php

return [
    'driver' => env('SESSION_DRIVER', 'file'),'lifetime' => 120,
    'expire_on_close' => false,
    'encrypt' => false,'files' => 
    'storage_path('framework/sessions'),'connection' => 'mysql',
    'table' => 'sessions',
    'store' => null,
    'lottery' => [2, 100],
    'cookie' => 'laravel_session',
    'path' => '/',
    'domain' => env('SESSION_DOMAIN', null),
    'secure' => env('SESSION_SECURE_COOKIE', false);
    'http_only' => true,
];

and this error I have

enter image description here

Upvotes: 1

Views: 609

Answers (1)

AddWeb Solution Pvt Ltd
AddWeb Solution Pvt Ltd

Reputation: 21681

I think you should follow below step wise for set SESSION_DRIVER as database

1- your .env file

# file = .env in your project root

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=testdb
DB_USERNAME=db_user
DB_PASSWORD=secret_pass

SESSION_DRIVER=database

Note the DB_CONNECTION setting here that you will need in next step.

2- update config/session.php file Where connection param should hold the string you used for DB_CONNECTION in .env file

# file = config/session.php

'driver' => env('SESSION_DRIVER', 'database'),
'connection' => 'mysql', // this is from DB_CONNECTION in .env file 

3- generate sessions table

php artisan session:table

// run the migration !!! very very important

php artisan migrate

4- if for some reason you decided to create the table manually without using migration , use this SQL. This is very important step, a wrong table will result in all kinds of problems. Primarily do not make the mistake of creating a table manually with id column as bigint as usual, session table is different. SQL for sessions table that you should run if you wanna create manually

DROP TABLE IF EXISTS `sessions`;
create table sessions
(
  id varchar(255) not null,
  user_id int(10) unsigned null,
  ip_address varchar(45) null,
  user_agent text null,
  payload text not null,
  last_activity int not null,
  constraint sessions_id_unique
  unique (id)
)

Please check i think this info help for you !!!

Upvotes: 2

Related Questions