×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

store session in database - Is there a simple method?

store session in database - Is there a simple method?

store session in database - Is there a simple method?

(OP)
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
http://www.fpgroups.com
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.

RE: store session in database - Is there a simple method?

(OP)
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
http://www.fpgroups.com
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.

RE: store session in database - Is there a simple method?

(OP)
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
http://www.fpgroups.com
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.

RE: store session in database - Is there a simple method?

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.

http://php.net/manual/en/function.serialize.php

http://php.net/manual/en/function.unserialize.php



----------------------------------
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

RE: store session in database - Is there a simple method?

(OP)
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
http://www.fpgroups.com
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close