Setting cookies like stack arrays


/ Published in: PHP
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.