Posted By

claudiowebdesign on 01/02/11


Tagged

php cookie array function


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

claudiowebdesign


Setting cookies like stack arrays


 / Published in: PHP
 

A little function used to set cookies as arrays of values instead of single combinations key->value. Note that it saves values as a stack.

  1. <?php
  2. function set_cronology($name,$value,$duration=7){
  3. $duration=time()+(3600*24*$duration);
  4. $max_stored_values=5;
  5. if(isset($_COOKIE[$name])){
  6. foreach($_COOKIE[$name] as $prop_crono=>$val_crono){
  7. if($val_crono==$value)
  8. return;
  9. }
  10. if($prop_crono<$max_stored_values)
  11. setcookie($name.'['.($prop_crono+1).']',$value,$duration);
  12. else{
  13. array_shift($_COOKIE[$name]);
  14. setcookie("$name[$max_stored_values]",$value,$duration);
  15. }
  16. }else
  17. setcookie($name.'[0]',$value,$duration);
  18. return;
  19. }
  20. ?>

Report this snippet  

You need to login to post a comment.