0

save additional data in Sessions Table in CakePHP 3?

once in a project i need to do the following tasks in CakePHP 3:

  • Logout users manually
  • Limit the number of sessions to one per user

I was using database sessions to accomplish that. and want to save additional data in sessions table.So below is the solution :

The session database model is a cake model like all the other models, which means you can interact with it, in the same way, by adding new columns to that table and/or deleting sessions if needed. Use the model object to update delete entities in that table (talking about cakephp 3.x)

Limiting the number of sessions to one per user can be tricky as sessions are created even if a user is not logged in. So you will have “user-less” sessions in your database as well.

Suggested way to tackle this

  • When a user logs in, get the current session ID and find the row in the session table that needs to be updated to include the username
  • At this time you may also want to delete the other rows that have the same user name, effectively destroying all the other sessions for this user.

Leave a Reply

Your email address will not be published. Required fields are marked *