Counting users logins on separate devices |
03-08-2018, 11:32 AM
(This post was last modified: 03-08-2018, 11:35 AM by ZeroNine. Edit Reason: typo )
I have a an application like many others where users are able to login to their account.
I have sessions stored in the database and everything is working smoothly. I realized that how the sessions work by default, under one user account, that user can technically log in to their account from many locations or devices at the same time. I know this because I can provide support by logging in as the user while they are logged in somewhere else. Knowing this, I was looking to figure a way out to monetize this by limiting this login count somehow and if a user wanted to log in with more than X amount of different locations on the same username/password account they would have to pay. Does anybody know how this would be possible?
1. Save user id and session data in database after user clicks log in
2. Every page visit, check database on how many connection they are making 2.1. If value exceeded X then logout first session (or how many you want) 3. User visits again and they need to login again due to the session have been destroyed
You can also check their IP Address, each device will have a different IP Address.
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
In your user login table store the session id of the user logged In. When user logs out destroy the session Id and remove session Id form login table. If some one else wants to logIn at the same time when some one is already logged in with the same username or password check for the session Id in the table if there is any session Id already in table block from loggin other wise allow
(03-08-2018, 12:33 PM)jreklund Wrote: 1. Save user id and session data in database after user clicks log in Thank you for your input and advice, What I'm concerned with this approach is adding a database call on each page would slow the site down..especially on a busy site. Also I know how to check if there is a session for user logging in but how do I check if another users session exists in the database? If I can search in the session database for another users USERID then I could start a count. (03-09-2018, 08:07 AM)jreklund Wrote:(03-08-2018, 03:11 PM)InsiteFX Wrote: You can also check their IP Address, each device will have a different IP Address. How did you know this??? Lol! Good guess, each company will use the same IP so I need to be able to count the user sessions coming from the same IP. (03-09-2018, 05:02 AM)Prateek Wrote: In your user login table store the session id of the user logged In. When user logs out destroy the session Id and remove session Id form login table. If some one else wants to logIn at the same time when some one is already logged in with the same username or password check for the session Id in the table if there is any session Id already in table block from loggin other wise allow Thank you for your input. I may be wrong but this solution has a loop hole because users may not manually log out. They can just close the browser and the sessions may time out. If that happens there is no trigger to update the database of that user logging out causing an in accurate count.
FYI, if you just want to know how many active sessions a user has at any given time, Community Auth does this out of the box. Logins are all records in the auth_sessions table, and with a simple select query you'd have what you need.
|
Welcome Guest, Not a member yet? Register Sign In |