Combine PHP session and MySQL for query login state -
my compeny current php website has users logging in using session. keeping field in session $_session['user_id'] , when logging out unset field. user data name, address, balance saved in mysql user table. want create query returns logged in user , balance on 500$.
how approach such task?
consider have lot of users looping through sessions in session folder , querying db , matching results in not possibility.
second option saving user login state in user table. setting 1 when user log in , 0 when log out. simplest option current code base , company bureaucracy. can think problem synchronization if session expire
third option transfer responsibility of session db session_set_save_handler.
what think best practice?
(i'd add @ofir_baruch said, avoiding multiple calls db in order update last user's loggin time)
add time-stamp "last login",in:
- user's table in db (lets call it: db's time-stamp)
- in user's session (lets call it: session's time-stamp)
(lets session lasts 15 minutes example) add concept when check if user's session valid:
(pseudo code)
when user request page: if session[user] not valid: create new session session[user] = username session[last-login] = time-stamp update user's last login column in db current time-stamp else if ( current_time_stamp - session[last-login] > 15 ) session[last-login] = time-stamp update user's last login column in db current time-stamp else do_nothing
this way, don't have update db's time-stamp each time user (like requesting page or refreshing), if 15 minutes have passed.
getting logged user's simple query now, @ofir_baruch described in comment.
Comments
Post a Comment