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.

 

$_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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.