Jump to content

$_SESSION possible types?


silkfire

Recommended Posts

Any value that can be serialized can be stored.  Arrays, strings, numbers, other basic data types are fine.  Most objects can also be serialized, but sometimes it can cause problems.  Best to test any object types to ensure they work.  Resource types are generally not capable of being serialized and thus cannot be saved in a session.

 

Note that PHP will not prevent you from attempting to store any type into the session array.  The values will just not be successfully saved and restored when moving to a different page.

 

Link to comment
Share on other sites

$_SESSION cookie

 

There's no such thing as a $_SESSION cookie. Cookies are data that is stored on the client. Session data is stored on the server. Only the session id is stored in a cookie on the client.

 

Objects can be assigned to a session variable ($_SESSION['some_name'] = $an_instance_of_an_object;) and even instigated in a session variable ($_SESSION['some_object'] = new class_name();) The only requirement is that the class definition must exist before the session_start() statement so that the object can be recreated correctly. There are even magic class methods available to let you manage what happens when objects are stored/retrieved (serialized/unserialized) to the session data file.

 

You can (temporarily) assign a resource to a session variable, but since all resources are destroyed when script execution ends on any page request, they cannot persist between page requests in a session variable. Resources must be (re)created on each page request.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.