Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-04-2012, 02:45 PM   PM User | #1
XmisterIS
Regular Coder

 
Join Date: Jun 2010
Posts: 239
Thanks: 50
Thanked 6 Times in 6 Posts
XmisterIS is an unknown quantity at this point
Is there any way to store resources in $_SESSION?

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!)
XmisterIS is offline   Reply With Quote
Old 01-04-2012, 02:48 PM   PM User | #2
BluePanther
Senior Coder

 
Join Date: Jul 2011
Posts: 1,226
Thanks: 3
Thanked 171 Times in 171 Posts
BluePanther is on a distinguished road
Sorry, but no
Quote:
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.
__________________
Useful function to retrieve difference in times
The best PHP resource
A good PHP FAQ
PLEASE remember to wrap your code in [PHP] tags.
PHP Code:
// Replace this
if(isset($_POST['submitButton']))
// With this
if(!empty($_POST))
// Then check for values/forms. Some IE versions don't send the submit button 
Quote:
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
BluePanther is offline   Reply With Quote
Old 01-05-2012, 11:23 AM   PM User | #3
XmisterIS
Regular Coder

 
Join Date: Jun 2010
Posts: 239
Thanks: 50
Thanked 6 Times in 6 Posts
XmisterIS is an unknown quantity at this point
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.
XmisterIS is offline   Reply With Quote
Old 01-05-2012, 01:39 PM   PM User | #4
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,500
Thanks: 44
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Quote:
Originally Posted by XmisterIS View Post
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.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Users who have thanked tangoforce for this post:
XmisterIS (01-05-2012)
Old 01-05-2012, 02:17 PM   PM User | #5
XmisterIS
Regular Coder

 
Join Date: Jun 2010
Posts: 239
Thanks: 50
Thanked 6 Times in 6 Posts
XmisterIS is an unknown quantity at this point
Quote:
Originally Posted by tangoforce View Post
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.
XmisterIS is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:21 AM.


Advertisement
Log in to turn off these ads.