Cookie Handler Class

Updated JRSofty 1 Tallied Votes 329 Views Share

This class allows for the handling of normal and serialized cookies as well as switching between these types of cookies.

Cookie Functions:

Write Cookies
Read Cookies
Delete Cookies

Use of this class is fairly simple.

Step 1: Include the class source in your php project using the require or include statement.
Step 2: Instance the cookie class by using the new keyword. The constructor uses these parameters

        cookieName[string]{required}        as the base name of the cookie
        cookieTimeout[int]{required}        as the time added the Unix timestamp to determine when a cookie expires
        cookieSerialize[bool]{optional}     default is false. If set to true then the cookies will be stored as a single serialized cookie.
        cookiePath[string]{optional}        default is "/" for root path you can set for sub-directory paths here if you wish.

The constructor also handles the switch over from individual cookies to serialzied cookies.

Step 3: Write a cookie

NOTE: It is important to remember that writing cookies must be done before any other output is sent to the browser.

The WriteCookie function takes as an array as an argument. This array should be built so that the name of the value name
is stored as the array element index and the value to be set is the element value

    array[name] = value

Create one element for each value you want stored as a cookie.

Step 4: Read a cookie

NOTE: Of course you can read a cookie from the $_COOKIE array however you will lose out on the in built handling of serialized cookies.

You read a cookie's value by calling the ReadCookie function which takes as its argument the value name and you will receive the
cookie value in return. If the value name is not present (this single cookie is not set or not in the serialized array) then a NULL
is returned.

It is best to store the value to a variable and then test for NULL by the is_null function.

Step 5: KillCookie

The KillCookie function allows you to delete a single cookie item or array element. It takes as its argument the value name to be deleted.

Step 6: DestroyAllCookies

Will go through all the cookies with the base name as set in the constructor and delete them.
The delete process for this and the KillCookie function is handled by setting the cookie's expire date as the current Unix timestamp - a large integer (several years).

Step 7: To change cookies from individual cookies to a serialized cookie
This is handled by changing the constructor's cookieSerialize flag from false to true. It must be done when the cookieClass is
instanced or it will not function correctly.

If you have any comments or questions concerning this php class you may contact me at (snip)

(snip)

LastMitch commented: Thanks for sharing +12
<?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
			}
		}
		
		
	}
?>
Member Avatar for LastMitch
LastMitch

If you have any comments or questions concerning this php class you may contact me at (snip)

I like this code snippet. It's a bit of a challenge. But I figure it out. Thanks for sharing.

0617b6e0062d749c9229570a86ba4284

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.