Session Question

Hi guys

Hope everyone is well.

I have been building a skeleton application which handles user authentication, registering, requesting new passwords and so forth. I will post it when it's ready as I would like some feedback on how I've built it and whether I've gone about things in the right way.

Anyway, my question is regarding sessions. I have set the application up to store sessions in a MySQL database. I was wondering if it was possible to add extra columns for the session system to store data such as last login time, session start time and current session time?

Thanks

Chris

Are you using CDbHttpSession? You can extend this class by overriding its writeSession() method.

Yes I am, what would be the best way of doing it?

Copy the original code and modify it. ;)

You mainly need to insert/update the extra column data.

lol oks, although I'm not really sure where to put the file that would do the overriding.

You can put the new class file anywhere, say, protected/components.

Then you just modify the app config and point the session class to this new class.

So I would need to copy the entire CDbHttpSession class into a new file or can I just override the methods I need too?

Also how would i set the config up to point to the new class? Can't find it in the documentation.

Sorry for the newbie questions.

Thanks :)

Chris

No, you just need to write a new class which extends CDbHttpSession. Your only code is the method to be overridden.

You already modified app config to use CDbHttpSession, right? Simply replace the 'class' option value to be your new class name.

Ah right, of course.

Thank you :)

This is also going to sound newbie, how do I access the data that is stored by writeSession, for example, extracting the name and id.

Chris

You may check readSession() on how the data is read and used. Since you are adding extra columns to this session table, I assume you have your own code trying to read and analyze these extra data (e.g. showing how many people are online currently).

That is what I'm building in now, readSession() returns a string of data, eg:

9f8839e839764195f5ad0adeafd11440__id|s:1:"1";9f8839e839764195f5ad0adeafd11440_name|s:6:"krak3n";9f8839e839764195f5ad0adeafd11440__states|a:0:{}

Is there an easy way of extracting the data held in this string like the id and name, or is it a case of using patterns?

Chris

No, you should not try to extract information from the data column because that is in some serialized format.

You said you have extra columns. How do you plan to make use of them?

If you only need to access the current user’s last login time, session start time, etc., you don’t need to override CDbHttpSession at all. All you need are just to store those information as user session data and retrieve them like you usually do with session data.

All i want to do atm is get the user id of the user the session is assigned too :(

If I'm going about this all wrong I apologize.

No apology. We just need to clarify the needs.

If you only want to know the current user id, why not using Yii::app()->user->id?

If you want to access all user IDs currently logged in, then you need to perform DB query on the session table and retrieve that user id column.

Of course, sometimes the most obvious thing is the thing I miss. I am so used to do doing things in a procedural way as a pose to OO this is a little new to me. I have used OO for php for a while but not to this extent.

Basically I just wanted to link the current session to the user, and then I can do the bits I need to do, which now I can.

Thank you

Chris