In PHP, is it considered best practice to store the complete object as a session variable?
From my experience, sometimes it works and sometime not. Is there any specific reason for it?
Example:
session_start();
$object = new sample_object();
$_SESSION['sample'] = $object;
Use serialize() in PHP before store your object, and call unserialize() when retrieve your object from session.
store object
session_start();
$object = new sample_object();
$_SESSION['sample'] = serialize($object);
retrieve object
session_start();
$object = unserialize($_SESSION['sample']);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With