CodeIgniter Forums
Counting users logins on separate devices - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Counting users logins on separate devices (/showthread.php?tid=70212)



Counting users logins on separate devices - ZeroNine - 03-08-2018

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?


RE: Counting users logins on separate devices - jreklund - 03-08-2018

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


RE: Counting users logins on separate devices - InsiteFX - 03-08-2018

You can also check their IP Address, each device will have a different IP Address.


RE: Counting users logins on separate devices - Prateek - 03-09-2018

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


RE: Counting users logins on separate devices - jreklund - 03-09-2018

(03-08-2018, 03:11 PM)InsiteFX Wrote: You can also check their IP Address, each device will have a different IP Address.

Each company will use the same IP-address. But they may have multiple users. So that can become a bad idea.


RE: Counting users logins on separate devices - ZeroNine - 03-15-2018

(03-08-2018, 12:33 PM)jreklund Wrote: 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

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.


RE: Counting users logins on separate devices - ZeroNine - 03-15-2018

(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.

Each company will use the same IP-address. But they may have multiple users. So that can become a bad idea.

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.


RE: Counting users logins on separate devices - ZeroNine - 03-15-2018

(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.


RE: Counting users logins on separate devices - skunkbad - 03-15-2018

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.