Return to Snippet

Revision: 340
at September 3, 2006 02:26 by ekobudisetiyo


Updated Code
class Cookie
{
   var $__setting;

   function __construct($name = 'FuseLogic',$live = 31104000)
   {
      $this->__setting = new open();
      $this->__setting->name = $name;
      $this->__setting->_cookies = array();
      $this->__setting->space = '___';

      $this->setDomain();
      $this->setPath();
      $this->setLive($live);

      if(is_array($_COOKIE))
      {
         foreach($_COOKIE as $k =>$v)
         {
            $temp = explode($this->__setting->space,$k);
            if($temp[0] == $this->__setting->name)
            {
               $k = str_replace($this->__setting->name.$this->__setting->space,'',$k);
               $this->__setting->_cookies[$k] = $v;
            }
         }
      }
   }
    
   function setDomain($domain = '')
   {
      $this->__setting->domain = $domain;
   }

   function setLive($time = 3600)
   {
      $this->__setting->live = (int) $time;
   }

   function setPath($path = '/')
   {
      $this->__setting->path = $path;
   }

   function name($name = null)
   {
      return $this->__setting->name.$this->__setting->space.$name;
   }

   public function __get($name)
   {
      return $this->__setting->_cookies[$name];
   }
  
   public function __set($name,$value)
   {
      $this->__setting->_cookies[$name] = $value;
      $name = $this->name($name);
      $_COOKIE[$name] = $value;
      if($value == null)
      {
         setcookie($name,$value,time()-$this->__setting->live,$this->__setting->path,$this->__setting->domain,0);
      }else
      {
         setcookie($name,$value,time()+$this->__setting->live,$this->__setting->path,$this->__setting->domain,0);
      }
   }

   public function __isset($name)
   {
      return isset($this->__setting->_cookies[$name]);
   }

   function clean()
   {
      foreach($this->__setting->_cookies as $name => $value)
      {
         $name = $this->name($name);
         setcookie($name,$value,time()-$this->__setting->live,$this->__setting->path,$this->__setting->domain,0);
         @$_COOKIE[$name] = null;
      }
      $this->__setting->_cookies = array();
   }

   function cleanAll()
   {
      foreach($_COOKIE as $name => $value)
      {
         setcookie($name,$value,time()-$this->__setting->live,$this->__setting->path,$this->__setting->domain,0);
      }
      @$_COOKIE = array();
   }

   function toArray()
   {
      return $this->__setting->_cookies;
   }

   function fromArray($a = null)
   {
      if(is_array($a))
      {
         foreach($a as $k => $v ) $this->$k = $v;
      }
   }

}

Revision: 339
at July 12, 2006 05:37 by ekobudisetiyo


Updated Code
class Cookie
{
   var $__setting;

   function __construct($name = 'FuseLogic')
   {
      $this->__setting = new open();
      $this->__setting->name = $name;
      $this->__setting->_cookies = array();
      $this->__setting->space = '___';

      $this->setDomain();
      $this->setPath();
      $this->setLive(60*60*24*3600);

      if(is_array($_COOKIE))
      {
         foreach($_COOKIE as $k =>$v)
         {
            $temp = explode($this->__setting->space,$k);
            if($temp[0] == $this->__setting->name)
            {
               $k = str_replace($this->__setting->name.$this->__setting->space,'',$k);
               $this->__setting->_cookies[$k] = $v;
            }
         }
      }
   }
    
   function setDomain($domain = '')
   {
      $this->__setting->domain = $domain;
   }

   function setLive($time = 3600)
   {
      $this->__setting->live = (int) $time;
   }

   function setPath($path = '/')
   {
      $this->__setting->path = $path;
   }

   function name($name = null)
   {
      return $this->__setting->name.$this->__setting->space.$name;
   }

   public function __get($name)
   {
      return @$this->__setting->_cookies[$name];
   }
  
   public function __set($name,$value)
   {
      $this->__setting->_cookies[$name] = $value;
      $name = $this->name($name);
      if($value == null)
      {
         setcookie($name,$value,time()-$this->__setting->live,$this->__setting->path,$this->__setting->domain,0);
      }else
      {
         setcookie($name,$value,time()+$this->__setting->live,$this->__setting->path,$this->__setting->domain,0);
      }
   }

   public function __isset($name)
   {
      return isset($this->__setting->_cookies[$name]);
   }

   function clean()
   {
      foreach($this->__setting->_cookies as $name => $value)
      {
         $name = $this->name($name);
         setcookie($name,$value,time()-$this->__setting->live,$this->__setting->path,$this->__setting->domain,0);
         @$_COOKIE[$name] = null;
      }
      $this->__setting->_cookies = array();
   }

   function cleanAll()
   {
      foreach($_COOKIE as $name => $value)
      {
         setcookie($name,$value,time()-$this->__setting->live,$this->__setting->path,$this->__setting->domain,0);
      }
      @$_COOKIE = array();
   }

   function toArray()
   {
      return $this->__setting->_cookies;
   }

   function fromArray($a = null)
   {
      if(is_array($a))
      {
         foreach($a as $k => $v ) $this->$k = $v;
      }
   }

}

Revision: 338
at July 10, 2006 03:08 by ekobudisetiyo


Initial Code
<?php
class Cookie
{
   var $__setting;

   function __construct($name = 'FuseLogic')
   {
      $this->__setting = new open();
      $this->__setting->name = $name;
      $this->__setting->_cookies = array();
      $this->__setting->space = '___';

      $this->setDomain();
      $this->setPath();
      $this->setLive(60*60*24*3600);

      if(is_array($_COOKIE))
      {
         foreach($_COOKIE as $k =>$v)
         {
            $temp = explode($this->__setting->space,$k);
            if($temp[0] == $this->__setting->name)
            {
               $k = str_replace($this->__setting->name.$this->__setting->space,'',$k);
               $this->__setting->_cookies[$k] = $v;
            }
         }
      }
   }
    
   function setDomain($domain = '')
   {
      $this->__setting->domain = $domain;
   }

   function setLive($time = 3600)
   {
      $this->__setting->live = (int) $time;
   }

   function setPath($path = '/')
   {
      $this->__setting->path = $path;
   }

   function name($name = null)
   {
      return $this->__setting->name.$this->__setting->space.$name;
   }

   public function __get($name)
   {
      return @$this->__setting->_cookies[$name];
   }
  
   public function __set($name,$value)
   {
      $this->__setting->_cookies[$name] = $value;
      $name = $this->name($name);
      if($value == null)
      {
         setcookie($name,$value,time()-$this->__setting->live,$this->__setting->path,$this->__setting->domain,0);
      }else
      {
         setcookie($name,$value,time()+$this->__setting->live,$this->__setting->path,$this->__setting->domain,0);
      }
   }

   public function __isset($name)
   {
      return isset($this->__setting->_cookies[$name]);
   }

   function clean()
   {
      foreach($this->__setting->_cookies as $name => $value)
      {
         $name = $this->name($name);
         setcookie($name,$value,time()-$this->__setting->live,$this->__setting->path,$this->__setting->domain,0);
         @$_COOKIE[$name] = null;
      }
      $this->__setting->_cookies = array();
   }

   function cleanAll()
   {
      foreach($_COOKIE as $name => $value)
      {
         setcookie($name,$value,time()-$this->__setting->live,$this->__setting->path,$this->__setting->domain,0);
      }
      @$_COOKIE = array();
   }

   function toArray()
   {
      return $this->__setting->_cookies;
   }

   function fromArray($a = null)
   {
      if(is_array($a))
      {
         foreach($a as $k => $v ) $this->$k = $v;
      }
   }

}
?>

Initial URL

                                

Initial Description
Easy and lazy way to use cookie in PHP5

Initial Title
Cookie Wrapper with name domain

Initial Tags

                                

Initial Language
PHP