Subcookies, the PHP version
Posted on 29 May 2007 (01:54 PM)
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:
All methods are static. This means you can use them like this:
CookieJar::bake('admin','Harmen',2);CookieJar::crumble('admin');- Download this code: /code/subcookies_php1.txt
The
subcookieObjparameter inSubCookieJar::bakeis an associative array, looking something like this:$subcookies = array ('name'=>'Harmen','age'=>22,'homepage'=>'whatstyle.net');- Download this code: /code/subcookies_php2.txt
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>';print $b;print '<br>';print $c;print '<br>';print $d;- Download this code: /code/subcookies_php3.txt
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.
Filed under PHP
- ← previous article: Take your cookies to the next level with Subcookies
- → next article: Peter-Paul Koch presents the Dutch Guild Of Front-Enders
Sorry, due to spam, comments are temporarily out of order.
Comments:
No comments have been added yet, you could be the first!