Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

store session in database - Is there a simple method? 1

Status
Not open for further replies.

southbeach

Programmer
Jan 22, 2008
879
US
I am about to start writing a procedure to store and extract session information in and out of MySQL.

The need arises due to need to capture user's last known session status to recover in the event of power failure or possible browse closure. The page is opened via a shortcut that looks like this

URL=http://mydomain.com/index.php?drawer=0001

each work station should have an assigned drawer ID thus it being the "key" to session recovery.

Now, a simple table (no problem) and a routine to dump the session content into it.

So, how do you dump $_SESSION into `table`.`column`?

As I post this, I am writing a foreach() loop to scan through the array, extract the key and value pairs and write that to the data and then will need to write the code to pair them back into an array ... when recovering the session



--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
Here are the snippets of code I'm using ...
Code:
function StoreSession($drawer=0) {
	if($drawer) {
		$conn=OpenDB('');
		$data='';

		// construct $data off $_SESSION
		$keys = array_keys($_SESSION);
		foreach ($_SESSION as $key => $val) 
		{
		   if(is_array($val)) { 
			 $data.='"'.$key.'" => array('.parseArray($val).'), ';
		   } else {
			$data.='"'.$key.'" => "'.$val.'", ';
		   }
		}
		$data.=')';
	
		$sql='INSERT INTO `srecovery` 
		(sessionid,accessed,drawerid,data) VALUES 
		("'.mysqli_real_escape_string($conn,session_id()).'",
		"'.mysqli_real_escape_string($conn,time()).'",
		"'.mysqli_real_escape_string($conn,$drawer).'",
		"'.mysqli_real_escape_string($conn,$data).'"
		) ON DUPLICATE KEY UPDATE 
		`sessionid`	=	"'.mysqli_real_escape_string($conn,session_id()).'",
		`accessed`	=	"'.mysqli_real_escape_string($conn,time()).'",
		`drawerid`	=	"'.mysqli_real_escape_string($conn,$drawer).'",
		`data`		=	"'.mysqli_real_escape_string($conn,$data).'"';

		$query=FetchQuery($sql, $conn);
		mysqli_close($conn);
	}	
}

function parseArray($array) {
	$data='';
	foreach ($array as $key => $val) 
	{
	   if(is_array($val)) { 
			$data.='"'.$key.'" => array('.parseArray($val).'), ';
	   } else {
		$data.='"'.$key.'" => "'.$val.'", ';
	   }
	}
	return($data);
}

May not be elegant but it is nicely constructing the array structure - Now, moving on to test if it works upon recovery!



--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
Well, that did not work ... lol

Had to change code so that each element of the array is written to MySQL table in key:value pair and if multidimensional, an additional field "masterkey" is stored as well.

So, when and if the need to recover is there, I can simply loop through the table and simply assign values to $_SESSION[] or $_SESSION[][] as needed.

To test this, I closed browser, I shutdown computer I even killed the session itself - Key is that the "shortcut" that opens the app is triggered with a ?drawer=#### so upon initial load, ['drawer'] is used to load a potentially "abnormally terminated session".

So far, I am happy with results as it appears to hold water!

--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
I would suggest you look into serialize(), and unserialize().

They will do what you want in a single function call. You can then store the output in a BLOB field on your DB.







----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Oh man, this is beautiful!

I am not regretting all I went through and having to remove the code to simplify it and follow your suggestion since in the process I "came up" with a workable solution and in the end, thanks to you, "learned the right way to do it" lol

Thanks!

--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top