Skip to content

Commit

Permalink
Set expiration to the past when deleting a cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Welch committed Mar 8, 2016
1 parent 3498278 commit bfc4230
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 34 deletions.
8 changes: 4 additions & 4 deletions CookiesPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ public function getDescription()
{
return 'A simple plugin for setting and getting cookies from within Craft CMS templates.';
}

public function getDocumentationUrl()
{
return 'https://github.com/khalwat/cookies/blob/master/README.md';
}

public function getReleaseFeedUrl()
{
return 'https://raw.githubusercontent.com/khalwat/cookies/master/releases.json';
}

public function getVersion()
{
return '1.0.2';
return '1.0.3';
}

public function getSchemaVersion()
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ All three of these methods accomplish the same thing:

## Changelog

### 1.0.3 -- 2016.03.08

* [Fixed] We now set the expiration date to the past if we're deleting a cookie, to force browsers to remove it
* [Improved] Updated the README.md

### 1.0.2 -- 2015.11.23

* Added support for Craft 2.5 new plugin features
Expand Down
9 changes: 9 additions & 0 deletions releases.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
[
{
"version": "1.0.3",
"downloadUrl": "https://github.com/khalwat/cookies/archive/master.zip",
"date": "2016-03-08T11:00:00-05:00",
"notes": [
"[Fixed] We now set the expiration date to the past if we're deleting a cookie, to force browsers to remove it",
"[Improved] Updated the README.md"
]
},
{
"version": "1.0.2",
"downloadUrl": "https://github.com/khalwat/cookies/archive/master.zip",
Expand Down
66 changes: 36 additions & 30 deletions services/Cookies_UtilsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,61 @@ class Cookies_UtilsService extends BaseApplicationComponent
{

/* --------------------------------------------------------------------------------
Standard cookies
Standard cookies
-------------------------------------------------------------------------------- */

public function set($name = "", $value = "", $expire = 0, $path = "", $domain = "", $secure = false, $httponly = false)
{
$expire = (int) $expire;
setcookie($name, $value, $expire, $path, $domain, $secure, $httponly);
$_COOKIE[$name] = $value;
$expire = (int) $expire;
/* -- Make sure the cookie expiry is in the past if we're deleting the cookie */
if (value=="")
$expire = (int)(time() - 3600);
setcookie($name, $value, $expire, $path, $domain, $secure, $httponly);
$_COOKIE[$name] = $value;
} /* -- set */

public function get($name = "")
{
if(isset($_COOKIE[$name]))
return $_COOKIE[$name];
if(isset($_COOKIE[$name]))
return $_COOKIE[$name];
} /* -- get */

/* --------------------------------------------------------------------------------
Security validated cookies
Security validated cookies
-------------------------------------------------------------------------------- */

public function setSecure($name = "", $value = "", $expire = 0, $path = "", $domain = "", $secure = false, $httponly = false)
{
if ($name == "")
{
craft()->request->deleteCookie($name);
}
else
{
$expire = (int) $expire;
$cookie = new HttpCookie($name, '');

$cookie->value = craft()->security->hashData(base64_encode(serialize($value)));
$cookie->expire = $expire;
$cookie->path = $path;
$cookie->domain = $domain;
$cookie->secure = $secure;
$cookie->httpOnly = $httponly;

craft()->request->getCookies()->add($cookie->name, $cookie);
}
if ($name == "")
{
craft()->request->deleteCookie($name);
}
else
{
$expire = (int) $expire;
/* -- Make sure the cookie expiry is in the past if we're deleting the cookie */
if (value=="")
$expire = (int)(time() - 3600);
$cookie = new HttpCookie($name, '');

$cookie->value = craft()->security->hashData(base64_encode(serialize($value)));
$cookie->expire = $expire;
$cookie->path = $path;
$cookie->domain = $domain;
$cookie->secure = $secure;
$cookie->httpOnly = $httponly;

craft()->request->getCookies()->add($cookie->name, $cookie);
}
} /* -- setSecure */

public function getSecure($name = "")
{
$cookie = craft()->request->getCookie($name);
if ($cookie && !empty($cookie->value) && ($data = craft()->security->validateData($cookie->value)) !== false)
{
return @unserialize(base64_decode($data));
}
$cookie = craft()->request->getCookie($name);
if ($cookie && !empty($cookie->value) && ($data = craft()->security->validateData($cookie->value)) !== false)
{
return @unserialize(base64_decode($data));
}
} /* -- getSecure */

} /* -- Cookies_UtilsService */

0 comments on commit bfc4230

Please sign in to comment.