jump to navigation

Cookie Component May 22, 2006

Posted by rossoft in CakePHP.
trackback

For easy working with cookies. It has encryption support through SimpleEncryption class

Installation:
1) Install the simpleEncryption
2) Copy the component to /app/controllers/components
3) Edit the component and change the $crypt_key to random text.

Usage: include the component in your controller. var $components=array(‘xxxx’,’cookie’);
——-
$value=array($login,$password);
$this->cookie->write(‘login’,$value,’+5 day’);
——-
$array=$this->cookie->read(‘login’);
——-
at logout()
$this->cookie->delete(‘login’);
——-

<?php

/**
* Cookie Component
* @author RosSoft
* @license MIT
* @version 0.13
*/

class CookieComponent extends Object
{
/**
* If not null, then the cookies will be encrypted
* with this key. Change to whatever you want.
*/
var $crypt_key=”CHANGE_THIS_TO_WHATEVER_YOU_WANT”;
var $crypt_engine=’Mcrypt’;

function startup(&$controller)
{
if ($this->crypt_key)
{
vendor(‘crypt’ . DS . ‘simple_crypt’);
$this->crypt=& new SimpleCrypt($this->crypt_engine);
}
}

/**
* Writes a cookie
* @param string $name Name of the cookie
* @param mixed $data Data to be written
* @param string $expires A valid strtotime string: when the data expires.
* @return boolean Success
*/
function write($name,$data=NULL,$expires=’+30 day’)
{
$data=serialize($data);
$time=strtotime($expires);

if ($this->crypt_key)
{
$data=$this->crypt->encrypt($this->crypt_key,$data);
}

if(setcookie($name,$data,$time,’/’))
{
return true;
}
else
{
$this->log(“CookieComponent: Write failed [$name][$data][$time]”);
return false;
}
}

/**
* Deletes a cookie
* @param string $name Name of the cookie
* @return boolean Success
*/
function delete($name)
{
if($this->write($name,”,’-999 day’))
{
return true;
}
else
{
$this->log(“CookieComponent: Delete failed [$name]”);
return false;
}
}

/**
* Reads a cookie
* @param string $name Name of the cookie
* @param boolean $unserialize Unserializes the content.
* Must be false if the cookie was not written through this component
*
* @return mixed Value of the cookie, or NULL if not exists
*/
function read($name,$unserialize=true)
{
if(isset($_COOKIE[$name]))
{
$string=$_COOKIE[$name];
if (get_magic_quotes_gpc())
{
$string=stripslashes($string);
}
if ($unserialize)
{
if ($this->crypt_key)
{
$string=$this->crypt->decrypt($this->crypt_key,$string);
}
$string=@unserialize($string);
return $string;
}
else
{
return $string;
}

}
else
{
return null;
}
}

/**
* Check if a cookie is set
* @param string $name Name of the cookie
* @return boolean The cookie exists
*/
function check($name)
{
return(isset($_COOKIE[$name]));
}

/**
* Removes all the cookies from the domain
* @author support at duggen dot net
*
* @link http://es.php.net/manual/en/function.setcookie.php#52081
*/
function clear()
{
$cookiesSet = array_keys($_COOKIE);
for ($x = 0; $x < count($cookiesSet); $x++)
{
if (is_array($_COOKIE[$cookiesSet[$x]]))
{
$cookiesSetA = array_keys($_COOKIE[$cookiesSet[$x]]);
for ($c = 0; $c < count($cookiesSetA); $c++)
{
$aCookie = $cookiesSet[$x].'[‘.$cookiesSetA[$c].’]’;
$this->delete($aCookie);
}
}
$this->delete($cookiesSet[$x]);
}
}
}

?>

Advertisements

Comments»

1. truster - June 12, 2006

I think
var $crypt_engine=”Simple”;
in definition and
$this->crypt=& new SimpleCrypt($this->crypt_engine);
in constructor should be nice bor initialization of this component in beforeFilter().

2. rossoft - June 12, 2006

var $crypt_engine is good idea (I’m lazy for updating the code of the post)

the component can’t be started at beforeFilter unless you do explicitly $this->Cookie->startup($this) in beforeFilter because startup is called always after the beforeFilter()

3. bkuhns - June 21, 2006

I love this cookie component and I can’t believe cake doesn’t have it built-in like the Session component is. Anyway, I recently had the need to check for the existance of a cookie created by this cookie component. To make things easier, I added to the component a check() method to mimic cake’s Session->check() method.

/**
* Check if a cookie is set
*
* @author Bret Kuhns
* @param String $name Cookie name to check for
* @return boolean True if the cookie exists
*/
function check($name) {
$expression = ‘return isset($_COOKIE[‘ . (is_numeric($name) ? $name : “‘$name'”) . ‘]);’;
return eval($expression);
}

4. rossoft - June 21, 2006

thanks I’ve added a similar code. But anyways, I think that this already worked because read returns null if not found

5. rossoft - June 21, 2006

updated, now setcookie sets the cookie domain-wide and check() method. thanks bkuhns

6. rikman - June 29, 2006

Thanks for this useful component – it saved me some time 🙂

7. ratibus - July 3, 2006

Hi

There is a little bug in the read() method.
In case of $unserialize not being true, you do not stripslashes() $_COOKIE[$name] if get_magic_quotes_gpc() returns true, although you should 😉

8. rossoft - July 3, 2006

thx, updated

9. tom - September 28, 2006

Uhm…. is there a text file of this for download? What you have here has curly quotes and the like, and therefore… would be a pain to use.

Thanks?

10. rossoft - September 28, 2006

there’s a compressed file linked in this post with some helpers and components. the cookie component is one of them https://rossoft.wordpress.com/2006/06/24/blog-tutorial-chapter-3-done/

11. http://sexshopgoosta.devhub.com - August 13, 2013

What’s up, this weekend is pleasant in support of me, since this time i am reading this enormous educational paragraph here at my home.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: