note 94390 added to function.setcookie
| From: | Eric at osu1 dot php dot net | Date: | Mon, 02 Nov 2009 06:57:42 +0000 |
| Subject: | note 94390 added to function.setcookie | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-162326@lists.php.net to get a copy of this message | ||
The server my php code is running on has sessions disabled so I am forced to store a fair bit of
arbitrary data in cookies. Using array names was impractical and problematic, so I implemented a
splitting routine. I do not serialize any class instances, just arrays and simple objects.
In a nutshell, when setting a cookie value, I serialize it, gzcompress it, base64 encode it, break
it into pieces and store it as a set of cookies. To fetch the cookie value I get the named piece
then iterate through piece names rebuilding the base64 data, then reverse the rest of the process.
The only other trick is deleting the pieces correctly.
Sessions are better, but if they are not available this is a viable alternative. I chose gz over bz
for compression because it looked faster with only slightly worse ratios.
Here is a simplified version of my implementation. This is a good starting point but is not
suitable for most uses. For example, the domain and path are hard coded and no return values are
checked for validity.
define( 'COOKIE_PORTIONS' , '_piece_' );
function clearpieces( $inKey , $inFirst ) {
$expire = time()-3600;
for ( $index = $inFirst ; array_key_exists( $inKey.COOKIE_PORTIONS.$index , $_COOKIE ) ; $index +=
1 ) {
setcookie( $inKey.COOKIE_PORTIONS.$index , '' , $expire , '/' , '' ,
0 );
unset( $_COOKIE[$inKey.COOKIE_PORTIONS.$index] );
}
}
function clearcookie( $inKey ) {
clearpieces( $inKey , 1 );
setcookie( $inKey , '' , time()-3600 , '/' , '' , 0 );
unset( $_COOKIE[$inKey] );
}
function storecookie( $inKey , $inValue , $inExpire ) {
$decode = serialize( $inValue );
$decode = gzcompress( $decode );
$decode = base64_encode( $decode );
$split = str_split( $decode , 4000 );//4k pieces
$count = count( $split );
for ( $index = 0 ; $index < $count ; $index += 1 ) {
$result = setcookie( ( $index > 0 ) ? $inKey.COOKIE_PORTIONS.$index : $inKey , $split[$index] ,
$inExpire , '/' , '' , 0 );
}
clearpieces( $inKey , $count );
}
function fetchcookie( $inKey ) {
$decode = $_COOKIE[$inKey];
for ( $index = 1 ; array_key_exists( $inKey.COOKIE_PORTIONS.$index , $_COOKIE ) ; $index += 1 ) {
$decode .= $_COOKIE[$inKey.COOKIE_PORTIONS.$index];
}
$decode = base64_decode( $decode );
$decode = gzuncompress( $decode );
return unserialize( $decode );
}
----
Server IP: 69.147.83.197
Probable Submitter: 71.237.136.91
----
Manual Page -- http://www.php.net/manual/en/function.setcookie.php
Edit -- https://master.php.net/note/edit/94390
Del: integrated -- https://master.php.net/note/delete/94390/integrated
Del: useless -- https://master.php.net/note/delete/94390/useless
Del: bad code -- https://master.php.net/note/delete/94390/bad+code
Del: spam -- https://master.php.net/note/delete/94390/spam
Del: non-english -- https://master.php.net/note/delete/94390/non-english
Del: in docs -- https://master.php.net/note/delete/94390/in+docs
Del: other reasons-- https://master.php.net/note/delete/94390
Reject -- https://master.php.net/note/reject/94390
Search -- https://master.php.net/manage/user-notes.php