PHP Classes
Icontem

File: cookie.class.php


  Search   All class groups All class groups   Latest entries Latest entries   Top 10 charts Top 10 charts   Newsletter Newsletter   Blog Blog   Forums Forums   Help FAQ Help FAQ  
  Login   Register  
Recommend this page to a friend! ReTweet ReTweet Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Jason Reed  >  Cookie Handler Class  >  cookie.class.php  
File: cookie.class.php
Role: Class source
Content type: text/plain
Description: Serialized Cookie Handler
Class: Cookie Handler Class
Store and retrieve serialized variables in cookies
 

Contents

Class file image Download
<?php
    
// cookieClass
    // Copyright (C) 2005 JRSofty Programming.
    // http://jrsofty1.stinkbugonline.com
    // Licensed under GNU/GPL 

    
class cookieClass{
        
        var 
$cName '';
        var 
$cTime '';
        var 
$cSerialize false;        
        var 
$cPath '';
        
        function 
cookieClass($cookieName$cookieTimeout$cookieSerialize false$cookiePath "/"){
            
$this->cName $cookieName;
            
$this->cTime $cookieTimeout;
            
$this->cSerialize $cookieSerialize;
            
$this->cPath $cookiePath;
            
// This should fix the issue if you have cookies set and THEN turn on the serialization.
            
$iname $this->cName "_S";
            if(
$this->cSerialize && !isset($_COOKIE[$iname])){
                
$cookArr = array();
                foreach(
$_COOKIE as $name=>$val){
                    if(
strpos($name,$this->cName) !== false ){ // make sure it is a cookie set by this application
                        
$subname substr($name,strlen($this->cName) + 1);
                        
$cookArr[$subname] = $val;
                        
$this->KillCookie($name);
                    }
                }
                
$this->WriteCookie($cookArr);
            }
            
// This is the opposite from above. changes a serialized cookie to multiple cookies without loss of data
            
if(!$this->cSerialize && isset($_COOKIE[$iname])){
                
$cookArr unserialize($_COOKIE[$iname]);
                
$this->KillCookie($iname);
                
$this->WriteCookie($cookArr);
            }
                        
                        
        }
        
        function 
DestroyAllCookies(){
            foreach(
$_COOKIE as $name=>$val){
                if(
strpos($name,$this->cName) !== false){
                    
$_COOKIE[$name] = NULL;
                    
$this->KillCookie($name);
                }
            }
        }
        
        function 
ReadCookie($item){
            if(
$this->cSerialize){
                
$name $this->cName "_S";
                if(isset(
$_COOKIE[$name])){
                    
// handle the cookie as a serialzied variable
                    
$sCookie unserialize($_COOKIE[$name]);
                    if(isset(
$sCookie[$item])){
                        return 
$sCookie[$item];
                    }else{
                        return 
NULL;
                    }
                }else{
                    return 
NULL;
                }
            }else{
                
$name $this->cName "_" $item;
                if(isset(
$_COOKIE[$name])){
                    
// handle the item as separate cookies
                    
return $_COOKIE[$name];
                }else{
                    return 
NULL;
                }
            }    
        }
        
        function 
KillCookie($cName){
            
$tStamp time() - 432000;
            
setcookie($cName,"",$tStamp,$this->cPath);
        }
        
        function 
WriteCookie($itemArr){
            if(
$this->cSerialize){                    
                
$sItems serialize($itemArr);
                
$name $this->cName "_S";
                
$_COOKIE[$name] = $sItems;
                
$tStamp time() + $this->cTime;
                
setcookie($name,$sItems,$tStamp,$this->cPath);
            }else{
                
$tStamp time() + $this->cTime;
                foreach(
$itemArr as $nam=>$val){
                    
$name $this->cName "_" $nam;
                    
$_COOKIE[$name] = $val;
                    
setcookie($name,$val,$tStamp,$this->cPath);
                }
e
            
}
        }
        
        
    }

 
  Advertise on this site Advertise on this site   Site map Site map   Statistics Statistics   Site tips Site tips   Privacy policy Privacy policy   Contact Contact  

For more information send a message to :
info at phpclasses dot org.
Copyright (c) Icontem 1999-2009 PHP Classes - PHP Class Scripts
  PHP Book Reviews - Reviews of books and other products