![]() |
Session Assistance/Advice - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Session Assistance/Advice (/showthread.php?tid=14446) |
Session Assistance/Advice - El Forum - 01-02-2009 [eluser]foxman149[/eluser] I love CI!!! This is my first post after reading the forum for a couple of months all of my questions have answers in here..all accept this one. I am using CI 1.7 sessions with DB enabled. My application is a ajax (jquery) heavy application. Heres the logic -I have a form that collections information for a search -I build the sql using active record and store the sql (not the data) in the session -I present the search results no problem -The results has a button to print the results in a pdf -I then grab the sql from session re run the query and build the pdf. This works beautifully but here is my problem. The session sql is not being updated all the time. I check the database and the query is there as it should be however when I run it the pdf prints my previous stored session sql. To test the session I print $this->session->userdata('sql') and it shows correctly. I can however get it to run perfect every time if I set $config['sess_time_to_update'] = 1 I am pretty sure setting this to 1 second is not good practise any suggestions? Is it really every second or just everytime the user makes a request? Maybe I should use some other implementations of sessions around the place and from most part that seems like what most people have done...thanks in advance. Session Assistance/Advice - El Forum - 01-03-2009 [eluser]majidmx[/eluser] whenever you want to replace the session data, try to unset the previous one and build it again. Session Assistance/Advice - El Forum - 01-03-2009 [eluser]foxman149[/eluser] Unfortunately This doesn't work either my options seem to be set the the $config[‘sess_time_to_update’] to 1 or use some variation of native session. Session Assistance/Advice - El Forum - 01-03-2009 [eluser]majidmx[/eluser] Whenever you request for the session data, if you log the session information in to a file, what will be there, the new session SQL or the old one ? Session Assistance/Advice - El Forum - 01-04-2009 [eluser]foxman149[/eluser] I have tested storing the session then immediately calling it to check if it has been set eg $this->session->set_userdata('query',$sql); print $this->session->userdata('query'); which does show the latest stored query however when I click the print pdf and I print out the session it is the old one. It kind of sounds to me like the data is cached or maybe the cookie...hmm I now if past ajax applications I have never had a problem using the "traditional" php session Session Assistance/Advice - El Forum - 01-04-2009 [eluser]foxman149[/eluser] The database is showing the correct query always..maybe it's the cookie....not getting updated...When you request the information does't the class use the database version using the cookie as a "key holder to do the look up should it not use the value that is stored in the dbase" Session Assistance/Advice - El Forum - 01-04-2009 [eluser]foxman149[/eluser] ok so I a digging in the session library and I found this // We only update the session every five minutes by default if (($this->userdata['last_activity'] + $this->sess_time_to_update) >= $this->now) { return; } which can be found in sess_update() about line 338. So basically the data will only update if the time is met which is set in the config file...but this is behaviour is strange why should I wait to store my new session data...If I explicitly call the session->set_userdata function shouldn't it update this immediately as I might want to use it. All the logic needs to do is if I call it update now not the next time around... in normal use I can see that updating the session data periodically makes sense however the logic should state if I update date it force the update Session Assistance/Advice - El Forum - 01-04-2009 [eluser]foxman149[/eluser] ok My little fix for this on line 338 of Session.php function sess_update($forceupdate=false) { // We only update the session every five minutes by default if (($this->userdata['last_activity'] + $this->sess_time_to_update) >= $this->now AND $forceupdate == false) { return; } // Save the old session id so we know which record to // update in the database if we need it $old_sessid = $this->userdata['session_id']; $new_sessid = ''; while (strlen($new_sessid) < 32) { $new_sessid .= mt_rand(0, mt_getrandmax()); } // To make the session ID even more secure we'll combine it with the user's IP $new_sessid .= $this->CI->input->ip_address(); // Turn it into a hash $new_sessid = md5(uniqid($new_sessid, TRUE)); // Update the session data in the session data array $this->userdata['session_id'] = $new_sessid; $this->userdata['last_activity'] = $this->now; // _set_cookie() will handle this for us if we aren't using database sessions // by pushing all userdata to the cookie. $cookie_data = NULL; // Update the session ID and last_activity field in the DB if needed if ($this->sess_use_database === TRUE) { // set cookie explicitly to only have our session data $cookie_data = array(); foreach (array('session_id','ip_address','user_agent','last_activity') as $val) { $cookie_data[$val] = $this->userdata[$val]; } $this->CI->db->query($this->CI->db->update_string($this->sess_table_name, array('last_activity' => $this->now, 'session_id' => $new_sessid), array('session_id' => $old_sessid))); } // Write the cookie $this->_set_cookie($cookie_data); } function set_userdata($newdata = array(), $newval = '',$update = false) { if (is_string($newdata)) { $newdata = array($newdata => $newval); } if (count($newdata) > 0) { foreach ($newdata as $key => $val) { $this->userdata[$key] = $val; } } $this->sess_write(); $this->sess_update($update); } Now to update before the time expires I just pass the extra param $this->session->set_userdata('query',$q,true); and it forces the update. I am a happy for any suggestions criticisms I am by no means a great programmer rather a pragmatic one...maybe even a solution that was already there |