/ Published in: PHP
Simple function to store an array in a cookie. Uses JSON.
To pull data out of cookie use 'json_decode()'. It can then be looped etc.
To pull data out of cookie use 'json_decode()'. It can then be looped etc.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php /** * Store an array inside a cookie as a JSON String * Useful for passing large bits of data :) * @author LiamC * @param $var = cookie value * @param $limit = size limit. default and max size for a cookie is 4kb. **/ function cookieArray($var, $limit=4096, $cookie_name="my_cookie") { //check if we have cookie if($_COOKIE[$cookie_name]) { //decode cookie string from JSON //push additional data //remove empty values //encode data again for cookie //need to limit cookie size. Default is set to 4Kb. If it is greater than limit. it then gets reset. //destroy cookie //set cookie with new data and expires after a week }else{ //create cookie with json string etc. //set cookie expires after a week }//end if }//end cookieArray ?>