I’m thinking of storing CI sessions in the database so I can display how many users are currently online, who specifically is online, etc.
Looking at http://codeigniter.com/user_guide/libraries/sessions.html, am I right in my understanding that information would be stored in the user_data column of the ci_session table? Meaning, maybe I just store the user’s id in there?
CodeIgniter will store the data in the table you specify in your config file. By default, it’s
ci_session. The session information, what is accessible through$_SESSIONfor instance, is serialized and saved in a column nameduser_data. That field will not be able to tell you whether or not the session has expired (or in other words, how many people are online).What you could do instead is use the
last_activitycolumn, which is a timestamp of the last time that session was active. You could run a SQL query that selects the count ofsession_idwhere thelast_activityis less than 2 minutes ago.With that said, an existing session doesn’t necessarily mean that the user is “signed in”. If you need to check that they’re signed in, you can use a
LIKEoperator to add a condition to theWHEREstatement that checks if a user is signed in. This will depend on what variable name you’re using so have a look at your data and see if you can figure it out.For example: