Welcome Guest, Not a member yet? Register   Sign In
Session serialized data... kinda? Dont understand the format
#21

(This post was last modified: 08-26-2015, 11:48 AM by jLinux.)

(08-26-2015, 08:55 AM)mwhitney Wrote: It sounds like you've gotten to a point where things are more complicated than they should be.

If I understand this correctly, you want to know which sessions are open for a given account_id. When the session library calls sess_regenerate(), it is essentially saying that the old session is closed and a new session has been opened using the existing data. So, when sess_regenerate() is called, you basically just need to notify your Accounts_model of this situation, which might be something like this:





PHP Code:
public function sess_regenerate($destroy FALSE)
{
 
   $oldId session_id();
 
   parent::sess_regenerate($destroy);
 
   $newId session_id();

 
   if (! class_exists('accounts_model'false)) {
 
       $this->load->model('accounts_model');
 
   }
 
   $this->accounts_model->sessionRegenerate($oldId$newId);


How you handle this in your Accounts_model depends on how you're using the relations in the database, but you shouldn't need to touch the session. If you need to keep the entry with the $oldId value until a write occurs, then I would recommend adding a nullable column to your table for the $newId value. Then, in Accounts_model->sessionRegenerate(), you would update the entry with $oldId to add the $newId value to the new column, and insert a new entry with the $newId in the normal session column. Then, when the write occurs, you would notify the Accounts_model and it could go back through and delete the row with the $newId in the new column. If you have the potential for regenerating the session multiple times before writing the session, you would simply have to go back through the chain, finding the rows with each of the previous session IDs and deleting all of them.

If the Accounts_model doesn't access the session in its constructor, this won't be a problem. Further, the only thing the model should really do in terms of accessing the session is store/retrieve the account_id in the session. Any time you access the account_id in the model, it should be through get/set methods which access the session only when the value hasn't been retrieved (on get) or when it changes (on set). The session ID shouldn't be needed in the Accounts_model except for those relationships, so the session ID can be passed to the model from the session library or retrieved from the reference table.

Actually my issues a little different.

Let me explain the end goal here.

So, this app will be able to be used via API, CLI or HTTP, and it can be setup so people can use the login more than once (EG: an API account "Service account", or something). So I need it so someone can login and see all the active sessions associated to their account, as well as whats going on with them, live.

I have it setup so CI will delete the old session when the session is regenerated, so there wont be a ton of old sessions polluting the sessions table. 

I have a MY_Session:Confusedess_regenerate() function, which just stores the "old" session in a public property, executes parent:Confusedess_regenerate(), then stores the "new" session in a public property. Then whenever the Account_model finally loads, it will check if the $new and $old are both populated, if so, then that means the session was regenerated, so take all the "activity" (NOT in the ci_sessions table) and change the session ID to the $new session id, thus allowing the user to see the activity that was going on for the same Login, even though it was a different session.

I created a MY_Session_database_driver::write(), which will execute parent::write(), then execute Accounts_model::assoc_session($_SESSION['account_id'], $session_id); to set the 'account_id' column in the 'ci_sessions' table to the current account ID.

I believe the problem im running into, is when the session is regenerated, it doesnt right away create a row in the ci_sessions table...

I tried to put some code inside the MY_Session:Confusedess_regenerate() to update the ci_sessions.account_id column of the current session ID to the proper account_id, but that still didnt fix the issue.

I put a lot of logging into the logs, and heres the logs and some comments on what they mean...
Quote:1) DEBUG - 2015-08-26 10:38:09 --> SESSION MY_Session:Confusedess_regenerate - Regenrating Session.. with destroy as true
// The sess_regenerate has been called, about to change the session
2) DEBUG - 2015-08-26 10:38:09 --> SESSION MY_Session:Confusedess_regenerate - Old Session: e54fdec78995bd44bae961aec00829fd1f5daf01
// MY_Session::$sess_info['old'] has been set to e54fdec78995bd44bae961aec00829fd1f5daf01
3) DEBUG - 2015-08-26 10:38:09 --> SESSION MY_Session:Confusedess_regenerate - New Session: 552c80f856600fab69cffcfcf6839b812412cf9a
// MY_Session::$sess_info['new'] has been set to 552c80f856600fab69cffcfcf6839b812412cf9a
4) DEBUG - 2015-08-26 10:38:09 --> SESSION Accounts_model::_check_regenerated_sess - Session was found to be regenerated
// The _check_regenerated_sess (which is in the controller) sees that the session has been regenerated, so it executes self::assoc_session..
5) DEBUG - 2015-08-26 10:38:09 --> SESSION Accounts_model::assoc_session - Associating OLD session data e54fdec78995bd44bae961aec00829fd1f5daf01 with NEW session 552c80f856600fab69cffcfcf6839b812412cf9a
// Now, all the activity in the account_activity table has had the session_id updated to the new session ID
// Now, i am going to attempt to update the ci_sessions.account_id to the current account ID where the ci_sessions.id = 552c80f856600fab69cffcfcf6839b812412cf9a, but first, query for it to make sure the record exists
6) DEBUG - 2015-08-26 10:38:09 --> SESSION Accounts_model::assoc_session - NO sessions found for session ID 552c80f856600fab69cffcfcf6839b812412cf9a
// Record does NOT exist
7) DEBUG - 2015-08-26 10:38:09 --> SESSION Accounts_model::get_active_sessions_data - No sessions in query for account 1
// No active session data found for the account, even though account_activity has a ton of info, no record in the ci_sessions table exists yet

So basically, when sess_regenerate is ran, it will change the session ID, but it doesnt actually write the new session ID record in the ci_sessions table yet..

So my question is, how can I have it do that when the session is regenerated? 

I know I COULD have MY_Session:Confusedess_regenerate(); create the new record in the table, then basically re-write all of CI_Session_database_driver::write() to a new MY_Session_database_driver::write(), which pretty much does the exact same thing, except it checks for the row to be existing before trying to create it, if it exists, it just updates it instead. But id like to overwrite as little CI code as possible (And I know Narff agres, lol)

I know I could try to set the CI_Session_database_driver::$_row_exists to TRUE, so that CI_Session_database_driver::write() will update the row, instead of create it, but right when CI_Session_database_driver::write()  is executed, it will reset it to FALSE.

Basically, all I really need, is for the session to be written to the database, right as its regenerated, (but then CI_Session_database_driver::write() will throw a fit because its trying to create a row that already exists)

Is there a way to have CI_Session_database_driver::write() execute when the session is regenerated? I know the CI_Session::_construct() uses session_set_save_handler() to set the handler, which will have write() eventually executed, I just need to have it done earlier, somehow

If anywhere between lines #3 and #6, I could have MY_Session_database_driver::write() execute, that would save the day.

Hope I made this clear enough... and thank you for the help!

P.S. I know all the methods/properties I referenced arent static, I just referenced them like that to make it easier to understand Smile
Reply


Messages In This Thread
RE: Session serialized data... kinda? Dont understand the format - by jLinux - 08-26-2015, 10:56 AM



Theme © iAndrew 2016 - Forum software by © MyBB