Subcookies, the PHP version

As a follow-up to my last article, Take your cookies to the next level with Subcookies, I wrote a PHP equivalent.


You can download the PHP file here as a Zip archive. The file contains two classes: CookieJar and SubCookieJar. Both work in the same way as their Javascript counterparts from the previous article. There are a few things to notice though:

  CookieJar::bake('admin','Harmen',2);
  
  CookieJar::crumble('admin');
  $subcookies = array (
  'name'=>'Harmen',
  'age'=>22,
  'homepage'=>'whatstyle.net'
  );

Also, note this little oddity:

if (CookieJar::fetch('admin'))
	CookieJar::crumble('admin');
		
SubCookieJar::bake('admin',array('name'=>'Harmen','age'=>22,'homepage'=>'whatstyle.net'),2);
$a = SubCookieJar::fetch('admin','name');
$b = SubCookieJar::fetch('admin','homepage');
$c = SubCookieJar::fetch('admin','age').'';
	
SubCookieJar::crumble('admin','age',2);	
$d = CookieJar::fetch('admin');
	
print $a;
print '<br></br>';
print $b;
print '<br></br>';
print $c;
print '<br></br>';
print $d;

The variable $c will not contain a value! I’m not sure why, ‘cause I fetched the value before crumbling the subcookie, but it’s not there. If anyone knows why this happens, please let me know.

For more information about the principles of subcookies, read my last article.