silkfire Posted February 3, 2012 Share Posted February 3, 2012 What types of data can the $_SESSION cookie store? Objects? Resources? And how are these stored in text form? Quote Link to comment https://forums.phpfreaks.com/topic/256320-_session-possible-types/ Share on other sites More sharing options...
trq Posted February 3, 2012 Share Posted February 3, 2012 $_SESSION is an array. It can store everything an array can, but I definitely would store any resources in it. Anything that can be safely serialised is safe. Quote Link to comment https://forums.phpfreaks.com/topic/256320-_session-possible-types/#findComment-1313976 Share on other sites More sharing options...
kicken Posted February 3, 2012 Share Posted February 3, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256320-_session-possible-types/#findComment-1313984 Share on other sites More sharing options...
PFMaBiSmAd Posted February 3, 2012 Share Posted February 3, 2012 $_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. Quote Link to comment https://forums.phpfreaks.com/topic/256320-_session-possible-types/#findComment-1314120 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.