note 37727 modified in function.serialize by aidan

From: Date: Thu, 24 Jun 2004 10:16:58 +0000
Subject: note 37727 modified in function.serialize by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-71573@lists.php.net to get a copy of this message
While writing shopping cart applications i needed a way to keep track of many variables beteween page loads.. i was using a cookie for each but this became hard to manage so I cam up with this simple session object. It serializes the object, base 64 encodes it and sets a cookie for it.. vice versa on page load where it detects the cookie add whatever variables you need to this class btw if you like it, hate it or have any comments send me an email to let me know <?php class session { var $cartID; var $userID; var $userAuth; function session () { //Attempt to load previous session from user cookie //First see if the session was passed through the URL //This is useful to pass stuff between different URL if ($_GET['session']) { $this = unserialize (base64_decode ($_GET['session'])); //Check for the cookie, if none a blank session is created } else { if ($_COOKIE['session']) { $this = unserialize (base64_decode ($_COOKIE['session'])); } } } function save () { setcookie ('session', base64_encode (serialize ($this))); } function delete () { setcookie ('session'); unset ($this); } } ?> --was-- While writing shopping cart applications i needed a way to keep track of many variables beteween page loads.. i was using a cookie for each but this became hard to manage so I cam up with this simple session object. It serializes the object, base 64 encodes it and sets a cookie for it.. vice versa on page load where it detects the cookie add whatever variables you need to this class btw if you like it, hate it or have any comments send me an email to let me know class session { var $cartID; var $userID; var $userAuth; function session () { //Attempt to load previous session from user cookie //First see if the session was passed through the URL //This is useful to pass stuff between different URL if ($_GET['session']) { $this = unserialize (base64_decode ($_GET['session'])); //Check for the cookie, if none a blank session is created } else { if ($_COOKIE['session']) { $this = unserialize (base64_decode ($_COOKIE['session'])); } } } function save () { setcookie ('session', base64_encode (serialize ($this))); } function delete () { setcookie ('session'); unset ($this); } } http://php.net/manual/en/function.serialize.php

« previous php.notes (#71573) next »