Keeping track on user in a PHP Server

Started by
1 comment, last by hplus0603 9 years, 10 months ago

Hi!

So after my last post conclusions, I decided on a PHP based server for my game

the server is pretty much done, the only problem is, that I have no idea how to keep track on logged users

I want to make sure that the same username is not used at the same time from different computers

At first I thought on saving a session key in the database and delete it when logging out, but what if the client crashed, or the connection was reset at the user`s end, I cant know when the user was disconnected

and I cant know how long a game session will last

I hope you can point me at the right direction,

Thanks!!

Advertisement
A typical approach is to have a fresh login invalidate all previous sessions belonging to that user. This avoids the problems you mentioned, as the new login will always be allowed, but multiple accounts will keep logging one another out.

One common way to implement this is to store sessions, and the-session-for-a-user, in memcache. The session should contain the user it belongs to.

Thus, when receiving a session cookie, the server reads the session out of memcache, if it's not there the session is invalid.

If it's there, then the server reads the current-session-for-user out of memcache, and if that is not the current session id, then the incoming session has been invalidated by another login.

If it's there, and the current session stored for the user matches the session ID, then it's the latest session and you should continue processing. The first thing you might want to do is extend the session and user key time-to-lives.

When logging in, you generate a session ID. You write this session ID as "the current session" for the user, using the user ID as key. Then you write the logged-in session information (including user information) to the session, using the session ID as key. Use keys with expiry time that's whatever you want for your session idle length -- say, half an hour, or thirty days, or whatever.

enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement