Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jun 2010
    Posts
    293
    Thanks
    63
    Thanked 8 Times in 8 Posts

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

  • #2
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    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.
    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 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • #3
    Regular Coder
    Join Date
    Jun 2010
    Posts
    293
    Thanks
    63
    Thanked 8 Times in 8 Posts
    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.

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,304
    Thanks
    57
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    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.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • Users who have thanked tangoforce for this post:

    XmisterIS (01-05-2012)

  • #5
    Regular Coder
    Join Date
    Jun 2010
    Posts
    293
    Thanks
    63
    Thanked 8 Times in 8 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •