note 14077 added to function.setcookie.php

From: Date: Mon, 16 Jul 2001 16:43:07 +0000
Subject: note 14077 added to function.setcookie.php
Groups: php.notes 
Request: Send a blank email to php-notes+get-15434@lists.php.net to get a copy of this message
To store an object as a cookie, I use the following: function object_to_cookie($obj) { $z = serialize($obj); $z = gzcompress($z); $z = base64_encode($z); $z = urlencode($z); return $z; } function cookie_to_object($cookie) { $z = urldecode($cookie); $z = base64_decode($z); $z = gzuncompress($z); $z = unserialize($z); return $z; } I don't think the urlencode()/urldecode() is strictly necessary (I only use it because '/' is part of the base64 alphabet and I'm paranoid). But this works well, and allows you to encode a lot of information into a single cookie instead of using multiple cookies; you can have something like a UserPrefs object that encapsulates all user-specific information, and set that as a single cookie. http://www.php.net/manual/en/function.setcookie.php

« previous php.notes (#15434) next »