...

View Full Version : Is there any way to store resources in $_SESSION?



XmisterIS
01-04-2012, 03:45 PM
I'm guessing the answer is probably "no" due to the nature of a resource, but can it be done by some clever wheeze that keeps hold of whatever connection the resource points to? (somehow!)

BluePanther
01-04-2012, 03:48 PM
Sorry, but no :(


Some types of data can not be serialized thus stored in sessions. It includes resource variables or objects with circular references (i.e. objects which passes a reference to itself to another object).

Cited from http://php.net/manual/en/intro.session.php. Explain what your end result would be, or why you want to pass a resource, and someone might be able to help you via a different medium than session.

XmisterIS
01-05-2012, 12:23 PM
Yep, I saw that in the manual, but thought there must be a way around this!

For example, I have an object that I store in session and that object contains a mysql connection resource as one of it's members.

At the moment, I use the resource indirectly via a wrapper function that ensures the resource is valid and, if not, re-establishes the connection before returning a valid resource.

It would be nice to be able to keep the resource open. Logically, I don't see that it's an impossible thing to do!

For the moment, I'll keep going with the wrapper function.

tangoforce
01-05-2012, 02:39 PM
It would be nice to be able to keep the resource open. Logically, I don't see that it's an impossible thing to do!


AFAIK (and especially mysql resources), resources are only valid for the lifetime of the script anyway. That means that as soon as the php script terminates, the mysql connection is dead. You cannot ressurrect it in another script instance - the php thread is gone, the mysql thread has been destroyed by the socket closure when php disconnected and it's automatically cleaned itself up.

Now imagine if you were to store a mysql connection resource in a session and then try to access it from multiple scripts that are triggered by a http call - you'd be in a right mess.

XmisterIS
01-05-2012, 03:17 PM
Now imagine if you were to store a mysql connection resource in a session and then try to access it from multiple scripts that are triggered by a http call - you'd be in a right mess.

Yes, that makes sense. One would, indeed, be in a veritable bugger's muddle. :rolleyes:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum