Advertisement
Blade83

PHP: Cookie get || set || delete Class

Nov 25th, 2012
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.44 KB | None | 0 0
  1. <?php
  2. /*
  3.  * VIRTUALWARS
  4.  *
  5.  * Die lecker schmeckende Cookie Variation!
  6.  *
  7.  * Author: $ Blade83
  8.  * Filename: cookie.class.php
  9.  *
  10.  */
  11.  
  12. class cookie
  13. {
  14.     protected
  15.         $path,
  16.         $del_time,
  17.         $domain;
  18.    
  19.     public
  20.         $coockie_contend,
  21.         $cookie_time;
  22.  
  23.     public function __construct()
  24.     {
  25.         $this->path         = "/"; # muss evt noch weiter ausgelesen werden
  26.         $this->cookie_time  = time() + (((60*60)*24)*14);
  27.         $this->del_time     = time() - (((60*60)*24)*14);
  28.         $this->domain       = $_SERVER['HTTP_HOST'];        // test.de:100
  29.         $splitdomain        = explode(":", $this->domain);
  30.         $this->domain       = $splitdomain[0];              // test.de
  31.     }
  32.  
  33.     public function get_cookie($name, $b64_decode=1)
  34.     {
  35.         if ($this->exists($name))
  36.         {
  37.             if($this->isempty($name) == FALSE)
  38.             {
  39.                 if($b64_decode==1)
  40.                     $this->coockie_contend = base64_decode($_COOKIE[$name]);
  41.                 else
  42.                     $this->coockie_contend = $_COOKIE[$name];
  43.             }
  44.         }
  45.         return (!empty($this->coockie_contend)) ? $this->coockie_contend: FALSE;
  46.     }
  47.  
  48.     public function set_cookie($name, $value)
  49.     {
  50.         $value = base64_encode($value);
  51.         if (!headers_sent())
  52.         {
  53.             $this->delete($name);
  54.             #setcookie($name, $value, $this->cookie_time, $this->path, $this->domain);
  55.             @setcookie($name, $value, $this->cookie_time, $this->path);
  56.         }
  57.     }
  58.     /*public function delete_login_cookies()
  59.     { this method is only used by vw!
  60.         $alle = array('pwd_lenghtco','savecookie','userco','pwdco','acopendivids','acgroupswithpersist');
  61.         for ($x=0; $x < count($alle); $x++)
  62.         {
  63.             $k = $this->delete($alle[$x]);
  64.         }
  65.     }*/
  66.    
  67.     public function delete($name)
  68.     {
  69.         $return = FALSE;
  70.         if (!headers_sent())
  71.         {
  72.             if ($this->exists($name))
  73.             {
  74.                 #@setcookie($name, "", $this->del_time, $this->path, $this->domain);
  75.                 @setcookie($name, "", $this->del_time, $this->path);
  76.                 $return = TRUE;
  77.             }
  78.         }
  79.         return $return;
  80.     }
  81.  
  82.     public function exists($name)
  83.     {
  84.         return (isset($_COOKIE[$name])) ? TRUE : FALSE;
  85.     }
  86.  
  87.     public function isempty($name)
  88.     {
  89.         return (empty($_COOKIE[$name])) ? TRUE : FALSE;
  90.     }
  91. }
  92.  
  93.  
  94. // Usage of this Class
  95.  
  96. $cookie = new cookie(); // create instance
  97.  
  98. $cookie->set_cookie('the_name_of_the_cookie', 'value_that_stored_in_cookie'); // this is a void method!
  99.  
  100. $value = $cookie->get_cookie('the_name_of_the_cookie'); // Returns the Value from the Cookie
  101.  
  102. $cookie->delete('the_name_of_the_cookie'); // Returns bool
  103.  
  104. $cookie->exists('the_name_of_the_cookie'); // Returns bool
  105.  
  106. ?>
  107. Thats it! Have a nice day!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement