PHP – Class Cookie Manager

This PHP Class can manage HTTP request cookies.

It can set a new cookie, check if a cookie was set and retrieve its value, and remove previously 
set cookies.

The package comes with different classes that can be used to adapt to different PHP versions and PHP 
configuration values.

Code Example: Download Example
index.php
<?php
// include and instantiate a CookieManager
include_once("CookieManager.class.php");
$cMgr = new CookieManager();

// create two simple cookies (one for prior to PHP 5.2.7 and one for the newest versions)
$cookie1 = new CookiePHP("myCookie", "cookie1value");
$cookie2 = new CookiePHP5("my2ndCookie", "cookie2value");

// add the cookies
$cMgr->add($cookie1);
$cMgr->add($cookie2);

// activate all the present cookies in the manager
// NOTE: by not supplying a $name value it activates all current cookies if they are not already activated
$cMgr->activate();

// create another cookie, let the manager determine the object type to use based on PHP version
$cMgr->create("anotherCookie", "cookie3value", time() + 3600, "", ".example.com", false, true);

// deactivate a specific cookie
// NOTE: by supplying a cookie's name it will deactivate only that cookie
$cMgr->deactivate("my2ndCookie");

// make sure it's deactivated
if (!$cMgr->activated("my2ndCookie"))
{
	// NOTE: you probably wouldn't want to do this with other cookie actions being performed afterwards
	echo "my2ndCookie deactivated successfully";
}

// make sure the cookie exists
if ($cMgr->exists("anotherCookie"))
{
	// activate it
	$cMgr->activate("anotherCookie");
	
	// verify
	if ($cMgr->activated("anotherCookie"))
	{
		// NOTE: you probably wouldn't want to do this with other cookie actions being performed afterwards
		echo "anotherCookie was successfully activated";
	}
}

// completely remove a cookie
$cMgr->remove("my2ndCookie");

// get direct access to a specific cookie
$aCookie = $cMgr->getCookie("anotherCookie");
// verify it's not activated
if (!$aCookie->status())
{
	// change its value and activate it
	$aCookie->Value = "changedValue";
	$aCookie->Secure = true;
	$aCookie->activate();
}
?>
CookieManager.class.php
<?php
if (!defined("PHP_VERSION_ID"))
{
	$version = explode(".", phpversion());
	define("PHP_VERSION_ID", ($version[0] * 10000) + ($version[1] * 100) + $version[3]);
}

interface iCookie
{
	function status();
	function activate();
	function deactivate();
}

class CookiePHP implements iCookie
{
	public $Name;
	public $Value;
	public $Expire;
	public $Path;
	public $Domain;
	public $Secure;
	
	public function __construct($name, $value, $expire = 0, $path = "", $domain = "", $secure = false)
	{
		$this->Name = $name;
		$this->Value = (string) $value;
		$this->Expire = $expire;
		$this->Path = $path;
		$this->Domain = $domain;
		
		if (is_bool($secure)) { $this->Secure = $secure; }
		else { $this->Secure = false; }
	}
	
	public function status()
	{
		return isset($_COOKIE[$this->Name]);
	}
	public function activate()
	{
		return setcookie($this->Name, $this->Value, $this->Expire, $this->Path, $this->Domain, $this->Secure);
	}
	public function deactivate()
	{
		return setcookie($this->Name, "", time() - 10800);
	}
}

class CookiePHP5 implements iCookie
{
	public $Name;
	public $Value;
	public $Expire;
	public $Path;
	public $Domain;
	public $Secure;
	public $HttpOnly;
	
	public function __construct($name, $value, $expire = 0, $path = "", $domain = "", $secure = false, $httponly = false)
	{
		$this->Name = $name;
		$this->Value = (string) $value;
		$this->Expire = $expire;
		$this->Path = $path;
		$this->Domain = $domain;
		
		if (is_bool($secure)) { $this->Secure = $secure; }
		else { $this->Secure = false; }
		if (is_bool($httponly)) { $this->HttpOnly = $httponly; }
		else { $this->HttpOnly = false; }
	}
	
	public function status()
	{
		return isset($_COOKIE[$this->Name]);
	}
	public function activate()
	{
		return setcookie($this->Name, $this->Value, $this->Expire, $this->Path, $this->Domain, $this->Secure, $this->Httponly);
	}
	public function deactivate()
	{
		return setcookie($this->Name, "", time() - 10800);
	}
}

class CookieManager
{
	private $_cookies;
	
	public function __construct()
	{
		$this->_cookies = array();
	}
	
	public function exists($name)
	{
		foreach ($this->_cookies as $cookie)
		{
			if ($name == $cookie->Name) { return true; }
		}
		return false;
	}
	public function add(iCookie $c)
	{
		if (!$this->exists($c->Name))
		{
			$this->_cookies[] = $c;
			return true;
		}
		return false;
	}
	public function create($name, $value, $expire = null, $path = null, $domain = null, $secure = false, $httponly = false)
	{
		if (!$this->exists($name))
		{
			$version = phpversion();
			if (PHP_VERSION_ID >= 50207) { $c = new CookiePHP5($name, $value, $expire, $path, $domain, $secure, $httponly); }
			else { $c = new CookiePHP($name, $value, $expire, $path, $domain, $secure); }
			$this->_cookies[] = $c;
			return true;
		}
		return false;
	}
	public function remove($name)
	{
		if ($this->exists($name))
		{
			$newCookies = array();
			foreach ($this->_cookies as $cookie)
			{
				if ($name != $cookie->Name) { $newCookies[] = $cookie; }
				else
				{
					if ($cookie->status()) { $cookie->deactivate(); }
				}
			}
			unset($this->_cookies);
			$this->_cookies = $newCookies;
			return true;
		}
		return false;
	}
	public function activated($name)
	{
		if (!$this->exists($name)) { return false; }
		else
		{
			if (PHP_VERSION_ID >= 40100) { return isset($_COOKIE[$name]); }
			else { return isset($HTTP_COOKIE_VARS[$name]); }
		}
	}
	public function activate($name = null)
	{
		if (null == $name)
		{
			foreach ($this->_cookies as $cookie)
			{
				if (!$cookie->status()) { $cookie->activate(); }
			}
			return true;
		}
		else
		{
			if (!$this->exists($name)) { return false; }
			else
			{
				$index = $this->getCookieIndex($name);
				if (-1 < $index)
				{
					if (!$this->activated($name))
					{
						$this->_cookies[$index]->activate();
						return true;
					}
				}
			}
		}
		return false;
	}
	public function deactivate($name = null)
	{
		if (null == $name)
		{
			foreach ($this->_cookies as $cookie)
			{
				if ($cookie->status()) { $cookie->deactivate(); }
			}
			return true;
		}
		else
		{
			if (!$this->exists($name)) { return false; }
			else
			{
				$index = $this->getCookieIndex($name);
				if (-1 < $index)
				{
					if ($this->activated($name))
					{
						$this->_cookies[$index]->deactivate();
						return true;
					}
				}
			}
		}
		return false;
	}
	public function getCookie($name)
	{
		if ($this->exists($name))
		{
			$index = $this->getCookieIndex($name);
			if (-1 < $index) { return $this->_cookies[$index]; }
		}
		return null;
	}
	
	private function getCookieIndex($name)
	{
		if ($this->exists($name))
		{
			$counter = 0;
			foreach ($this->_cookies as $cookie)
			{
				if ($name == $cookie->Name) { return $counter; }
				$counter++;
			}
		}
		return -1;
	}
}
?>

Post to Twitter Post to Digg Post to Facebook Post to Google Buzz Send Gmail

Leave a Comment

Your email address will not be published. Required fields are marked *