C
C
Cuthbert Allgood2017-05-25 22:02:47
Yii
Cuthbert Allgood, 2017-05-25 22:02:47

PHP: How to remove user from database after session expires?

Good evening.
Background : I'm doing a simple chat. For registration, the user specifies only a nickname. Nickname - not unique, there can be several users with the same nickname. When registering quickly, the user is added to the database, when logging out, the user is deleted. The user can create a room or enter rooms. If the user leaves the room last, it is deleted.
Problem : if the user entered the room, left the computer and his session ended. As a result, there remains a dead room with a dead user in the database.
Question: how to solve such a problem? Maybe there are some standard functions? Or can you look at some example? It is necessary to somehow check if the user's session has ended, then delete him from the database.
I am using the YII2 framework.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
dinegnet, 2017-05-25
@dinegnet

1. Consider that a rotten session = a missing entry.
2. According to the crown, clean out all the rotten ones. How often - decide for yourself based on the data on the degree of workload of your system.

T
ThunderCat, 2017-05-25
@ThunderCat

When registering quickly, the user is added to the database, when logging out, the user is deleted.
Don't you think that there is some extra movement here? Isn't it easier to store all that crap that you write to the database in the session during registration?
As an option - store in a radish, there is a lifetime of the record, everything that is dead will fall off by itself on a timeout.

S
Sergey Sokolov, 2017-05-25
@sergiks

You need a timeout user delete event. To use it to check the user's rooms to see if they can be deleted at the same time.
Those. so that the occurrence of a certain time causes the execution of the procedure.
There is no such mechanism in PHP. Therefore, it remains to look at the clock every N minutes and decide what to do.
upd. You can use Redis instead of a database - there is an Expire mechanism (deleting obsolete keys). With each user event, the time of dying of his session is postponed. You should probably do the same with the user's rooms: at each user event, at the same time update all his rooms.
You will have to check every, say, 5 minutes all sessions: which of the sessions it is time to delete + check their rooms.

X
xmoonlight, 2017-05-25
@xmoonlight

the Activity table, which stores the last user activity.
If the next action was with an interval exceeding the session time - delete the room.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question