note 37727 deleted from function.serialize by aharvey
| From: | aharvey@php.net | Date: | Tue, 06 Nov 2012 02:03:16 +0000 |
| Subject: | note 37727 deleted from function.serialize by aharvey | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-191742@lists.php.net to get a copy of this message | ||
Note Submitter: tino at infeon dot com
----
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);
}
}
?>