Im Using CakePHP 2.2.1 and im trying to improve my User Authentication with the Auth Component. When Users try to log in from multiple locations they get individual session IDs, what i want to do is kill the old session so the user cannot log in from multiple locations at the same time.
I converted how CakePHP saves its sessions using this post cakephp prevent user login from multiple locations at the same time but no answer was given on how to kill off the old session when the new one is created.
I thought about creating a Session Model and using that to select the records but im not sure if thats a safe route to go with.
I also read through the CakePHP documentation on the Session Component and CakeSession Datasource hoping there might be a hint but i wasnt able to find anything.
Any advice would be greatly appreciated.
Generally, you want to switch Session handling to Database, so you can delete stale sessions when you detect the same user logs in with a different
session_id.The steps, to give you an idea:
Switch Session handling to Database
Create
cake_sessionstableYou would then see the following:
Assuming you bind
session_idtouser_idbyIterate through
datafield at your session database and delete the row off if the sameuser_identers your site and with a differentsession_id.Unfortunately, CakePHP stores
dataasserialize()-ed data. You will have to either iterate through each of the rows atcake_sessionstable to look for matchinguser_idcontained in seralizeddatato delete.Or, just to give you an idea, you can use the following SQL for an approximate method to delete the associated row:
That way the old user who has the old
session_idwill not be able to continue on the site as the logged in user.