My compeny current PHP website has users that are logging in using session. Keeping a field in the session $_SESSION['user_id'] and when logging out unset this field. The user data like Name, Address, Balance is saved in MySQL user table. Now I want to create a query that returns all the logged in user and Balance is over 500$.
How would you approach such task?
Consider that I have a lot of users so looping through all the sessions in session folder and than querying the DB and than matching the results in not really a possibility.
Second option is saving user login state in the user table. setting it to 1 when user log in and to 0 when log out. This is the simplest option to do with current code base and the company bureaucracy. But I can think problem with synchronization especially if the session expire
Third option is to transfer all the responsibility of the session to the DB with something like session_set_save_handler.
What do you think is the best practice?
last_login(I believe it's more useful than just aboolean- logged or not field) and then you can define a "buffer time", let's say: Logged = last_login (unix timestamp) + 60*5 (5 minutes). Now you can use a simple SQL query.