alexks wrote:Yes, we are building something like myspace and expect to have a lot of users. Where are session variables get stored? Can a long array cause memory problems?
Session variables are stored in files on the webserver. Basically, I like to think of session files as gym lockers. When you register a session in php, the end user is given a session id (gym locker combination) in the form of a cookie. Subsequent requests (each browser request returns the cookie with the session id) gives the end-user access to the contents of the file where his or her information is stored. Thus the end user's browser only stores the unique session id and all the rest of the information is stored on the webserver. If you really do have a myspace clone on your hands, you really need to consider if you can handle sticking those large arrays into files for thousands of users. Also, at some point you may decide you need to load balance your website across several servers (cluster.) If they share the load in a round-robin fashion, your users cannot stay autheticated with sessions because the session is written to one machine at a time.
Basically, you can store sessions in one of 3 ways.
- The default, files see session.save_path
Pros: default, easy to setup. Cons: security on shared host. Disk space. Doesn't cluster.
- Store sessions in database. See session_set_save_handler
Pros: Clusters well because all sessions are stored in the database. Session cleans up for you (well, you make the garbage collection function actually.) Cons: a little tricky to setup but I'm sure you can google some examples. Possible locking errors as all script threads access database tables simultaneously ( you can do table locks, etc. )
- Use memcache. This is a library that php can use for caching anything. See PHP Memcache Functions. I assume, this uses the session_set_save_handler method to integrate with sessions.
Pros: Very fast. clusters well as cached data can be stored on multiple machines. Stores in fast RAM. Cons, sufficient memory required.
Here is a discussion about session performance you may want to read.
alexks wrote: And how to find out which user is inactive. If we put timeout for a user session and a user stays longer than that time, then that user will be logged out without actually logging out.
Again, the session can do garbage collection on inactivity. When an end user closes his or her browser, the php session cookie is erased (php session handling does not instruct the browser to cache the cookie until a future date.) Thus the next time the end user goes to the website, he or she is issued a new cookie and cannot access the information from the previous session file. Eventually (90 minutes is the default?...I always forget) the session file is sent to garbage collection (erased) after an inactivity limit is reached.
As for detecting a browser being closed, I'd recommend not worrying about it and letting the automatic garbage collection clean up.