Share this page: Follow me to be notified of new articles:

Good cookie (Memcache version)

I don't need cookies!

What is it?

Firefox announced that starting with version 22, it'll unallow third parties to set cookies on websites (learn more).
It means that only the website that you are visiting (or websites you visited) can store cookies on the user's computer. Other scripts like Google Analytics or ad-network's scripts can't store cookies anymore.
"Good cookie" solves that issue : we use "real" cookies as much as possible and we store them on the server "fake" cookies when needed.

What does it do?

  1. You can create, read and delete (one or all) cookies for your users, whether they accept cookies or not. A "fake" cookie system on the server-side can store cookies for servers that block them. It's 100% transparent for the users and the website.
  2. Depending on the user's browser version, the script decides if we can create "real" cookies for the user or if we need to store "fake" cookies on the server.
  3. The script lets you set and use (read, delete) cookies. You don't have to worry if the user is accepting "real" cookies or not. The script does it for you and manages both cases automatically.
  4. "Fake" cookies can have an expiration date (timestamp), just like "real" cookies.
  5. The script will try to leave a cookie on users we presume that block cookies. If it works, an automatic switch will convert all "fake" cookies into "real" cookies!

Notable differences

  1. "Fake" cookies are available immediately after being set as opposed to "real" cookies for which you need to reload the page to access them.

What do I need?

  1. PHP 4/5
  2. MemCache
If you wish to use Redis instead of MemCache, you'll find a Redis version of Good cookie here.

What can I do with the source code?

What ever you want. (learn more)
Would you like to improve it? If you want, send me the updated version and if you want, I'll publish it here. (contact me)

Changelog

Source code

Examples

Create a cookie:
<?php
// Starting MemCache
$Memcache = new Memcache;
$Memcache->connect('localhost', 11211);

// Set up a cookie
// Equivalent of : setcookie('name', 'cookie value', time() + 3600);
GoodCookieSet($Memcache, 'name', 'cookie value', time() + 3600);

// Closing Memcache
$Memcache->close();
?>

Read a cookie:
<?php
// Starting MemCache
$Memcache = new Memcache;
$Memcache->connect('localhost', 11211);

// Read a cookie
// Equivalent of : echo $_COOKIE['name'];
echo GoodCookieRead($Memcache, 'name');

// Closing Memcache
$Memcache->close();
?>

Get an array with all cookies:
<?php
// Starting MemCache
$Memcache = new Memcache;
$Memcache->connect('localhost', 11211);

// Get all cookies in an array
// Equivalent of : print_r($_COOKIE);
$AllCookies = GoodCookieGetAll($Memcache);
print_r($AllCookies);

// Closing Memcache
$Memcache->close();
?>

Delete a cookie:
<?php
// Starting MemCache
$Memcache = new Memcache;
$Memcache->connect('localhost', 11211);

// Delete a cookie
// Equivalent of : setcookie('name');
GoodCookieDelete($Memcache, 'name');

// Closing Memcache
$Memcache->close();
?>

Delete all cookies:
<?php
// Starting MemCache
$Memcache = new Memcache;
$Memcache->connect('localhost', 11211);

// Delete all cookies
GoodCookieDeleteAll($Memcache);

// Closing Memcache
$Memcache->close();
?>

Display debug informations:
<?php
// Starting MemCache
$Memcache = new Memcache;
$Memcache->connect('localhost', 11211);

// Display debug informations
echo GoodCookieDebug($Memcache);

// Closing Memcache
$Memcache->close();
?>
Last update of this page: March 27, 2017
Please do not copy any of the content of this website without asking me first.