PHPEdit.net Community
   
1 2 3 4
Tutorials Tips Pasties Code Snippets
 

Home > Code snippets > PHP > Cookie Class

Cookie Class

Created by emiliodeg, last update on 07/07/2008 15:15

Cookie Class very easy to use   set cookie: Cookie::set('car','abc 123'); get cookie: Cookie::get('car'); //return 'abc 123' del cookie: Cookie::del('car'); //delete cookie car del all cookies: Cookie::delAll(); isset cookie??: Cookie::exists('car'); //return bool value empty cookie??: Cookie::isEmpty('car'); //return bool value

  1. class Cookie{
  2. public static function
    set($cookieName,$value='',$time=3600,$path=null,$domain=null,$httpOnly
    =false){

  3. setcookie($cookieName,$value,time()+(int)$time,$path,$domain,(bool)$ht
    tpOnly
    );
  4. $_COOKIE[$cookieName]=$value;
  5. }
  6. public static function get($cookieName,$defaultValue=''){
  7. return
    self::exists($_COOKIE[$cookieName])?$_COOKIE[$cookieName]:$defaultValu
    e
    ?$defaultValue:false;
  8. }
  9. public static function
    del($cookieName,$value='',$time=-3600,$path=null,$domain=null,$httpOnl
    y
    =false){

  10. self::set($cookieName,$value,$time,$path,$domain,$httpOnly);
  11. unset($_COOKIE[$cookieName]);
  12. }
  13. public static function exists($cookieName){
  14. return isset($_COOKIE[$cookieName]);
  15. }
  16. public static function isEmpty($cookieName){
  17. return empty($_COOKIE[$cookieName]);
  18. }
  19. public static function delAll(){
  20. $count=count($_COOKIE);
  21. $names=array_keys($_COOKIE);
  22. for($i=0;$i<$count;$i++){
  23. self::del($names[$i]);
  24. }
  25. }
  26. }

Dependencies

No special requirements are needed by this snippet

By logging in you will be able to:

  • Recommand this page to someone else
  • Monitor changes on this item
  • Rate this item
  • Post comments
  • Download this item
Login now!

 
PHPEdit User Community, © 2008 WaterProof SARL
Powered by PHPEdit