chimby Posted November 4, 2006 Share Posted November 4, 2006 >:( I am going crazy with this problem to I thought I'd ask...I have some web forms that need to store their data in separate sessions until each form is ready to be submitted. The idea is that you can pick which session you want from a drop down list and have that session and it's data restored. I have the session_set_save_handler() function redefined to store the session data in MySQL, but I am having a chicken and egg problem trying to query the DB for an old session and restoring it. Here is my write handler:[code]function _write($id, $data){ global $_sess_db; mysql_select_db('forms', $_sess_db); if ($_GET['pat']){ $patID = $_GET['pat']; } else if ($_POST['patID']){ $patID = $_POST['patID']; } else if ($_SESSION['PatNum']){ $patID = $_SESSION['PatNum']; } if ($_SESSION['curLocation']){ $curLocation = $_SESSION['curLocation']; } if ($_SESSION['curForm']){ $curForm = $_SESSION['curForm']; } $access = time(); $id = mysql_real_escape_string($id); $access = mysql_real_escape_string($access); $data = mysql_real_escape_string($data); $sql = "REPLACE INTO sessions VALUES ('$id', '$access', '$data', '$patID', '$curLocation', '$curForm')"; return mysql_query($sql, $_sess_db);}[/code]I know it is very hackish, but the idea is that the patID, curLocation, and curForm are stored in the table along with the session id and data. First I query the db to based the patID, curLocation, and curForm to retrieve the appropriate session id and data. Next I want to leave my current session and restore the old one. How can I cleanly end my current session (basically I just want to LEAVE the session without destroying it or changing the session variables) and continue where I left off in the other sessionthank,dt Link to comment https://forums.phpfreaks.com/topic/26119-session-insanity/ Share on other sites More sharing options...
toplay Posted November 4, 2006 Share Posted November 4, 2006 Is the first column, where you're using $id, an autoincrement column?You know that the REPLACE will delete an existing row if it exists and add a new row, and if that $id column is an autoincrement, it will use the next autoincrement number and not the value that's supplied in $id.Basically, changing sessions involves setting the session_name($sessionYouWant), and session_id($sessionIDYouWant) to the correct session BEFORE issuing a session_start().It's good coding habit to always list column names in MySQL inserts/replace and updates. In the future, when upgrading/changing you may need to add columns to table(s), in which case it may break existing code.FYI - Look at my class.DB_eSession code (from 2004) using session_set_save_handler() for insight/learning. You'll have to join (for free) to download source:http://www.phpclasses.org/browse/package/1624.html Link to comment https://forums.phpfreaks.com/topic/26119-session-insanity/#findComment-119460 Share on other sites More sharing options...
chimby Posted November 6, 2006 Author Share Posted November 6, 2006 No the first column ($id) is not an auto increment. It is the actual session id.I think you are on to something with the REPLACE statement. It must be deleting the row before the session_read function is called. Then it would create a new session, but with the same session name as the old one. What do you think? Link to comment https://forums.phpfreaks.com/topic/26119-session-insanity/#findComment-120607 Share on other sites More sharing options...
chimby Posted November 7, 2006 Author Share Posted November 7, 2006 ???Still stuck on this one.I query for a session.I try to resume the session by setting the session ID to the old one.I do a session_start()The problem is that the session variables are wiped out in the old session and replaced with the session variables of the session I am leaving.Any thoughts??? Link to comment https://forums.phpfreaks.com/topic/26119-session-insanity/#findComment-121028 Share on other sites More sharing options...
chimby Posted November 8, 2006 Author Share Posted November 8, 2006 ;D I've figured it out....Just in case anyone else has a similar problem I thought I'd share my solution.What worked for me was to manually run session_decode() on the data string found in the initial session query. I thought that session_start() would automatically read the data from the found session, but that wasn't the case. Here is the working code to detect and resume an old session found in the database.[code]if($_GET['pat'] && !$_POST['PatNum']){ //various conditionals.... session_id(time()); //create a temporary session to query the db for a session with a given location(form) and person session_start(); mysql_select_db('forms'); $sessionQuery = "SELECT * FROM sessions s WHERE s.patID='" . $_GET['pat'] . "' AND s.curForm='" . $_GET['location'] . "'"; $sessionResult = mysql_query($sessionQuery); $sessionData = mysql_fetch_array($sessionResult); if ($sessionData['patID']){ $ssid = $sessionData['id']; //session_write_close(); //DON"T DO THIS...There is an apparent bug in php that destroys "user" session handlers when this is run //session_destroy(); //DON'T DO THIS...There is an apparent bug in php that destroys "user" session handlers when this is run session_id($ssid); session_start(); session_decode($sessionData['data']); //THIS IS WHAT WAS MISSING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! } else { session_regenerate_id(); session_start(); }} else { //session_id('nothing'); session_start();}[/code]Let me know if anyone needs this but doesn't understand it. Link to comment https://forums.phpfreaks.com/topic/26119-session-insanity/#findComment-121590 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.