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

Good cookie (Redis version)

I don't need cookies!

What is it? What does it do?

Please visit the Good Cookie original version page (using Memcache) to learn more about Good cookie.

Why Redis? What are the notable differences with the MemCache version? How do I migrate?

  1. MemCache is a very good tool. However, every time you restart Memcache you lose all your data. Because you probably want to store cookies for a few days/week, you take a risk of losing everything should you want or have to restart Memcache. Redis offers a useful tool to save the database on the disk frequently. (read more)
  2. Redis does not offer the possibility to store PHP Arrays like MemCache. I had to rewrite a large part of the functions for the Redis version. The functions will provide the same results as the MemCache version but the data is not stored the same way at all.
  3. Migration info: I did not develop a migration tool from the MemCache version to the Redis version of Good cookie. To migrate from the MemCache version to the Redis version you just have to replace the function and replace the MemCache connection with a Redis connection (but you'll lose all "fake" cookies stored in MemCache).

What do I need?

  1. PHP 4/5
  2. Redis
  3. PhpRedis (PhpRedis is a client library for the Redis key-value store)
If you wish to use MemCache instead of Redis, you'll find a Memcache 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
// Redis connection
$Redis = new Redis();
$Redis->pconnect('127.0.0.1', 6379); // host, port
$Redis->select(15);  // switch to DB X

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

Read a cookie:
<?php
// Redis connection
$Redis = new Redis();
$Redis->pconnect('127.0.0.1', 6379); // host, port
$Redis->select(15);  // switch to DB X

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

Get an array with all cookies:
<?php
// Redis connection
$Redis = new Redis();
$Redis->pconnect('127.0.0.1', 6379); // host, port
$Redis->select(15);  // switch to DB X

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

Delete a cookie:
<?php
// Redis connection
$Redis = new Redis();
$Redis->pconnect('127.0.0.1', 6379); // host, port
$Redis->select(15);  // switch to DB X

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

Delete all cookies:
<?php
// Redis connection
$Redis = new Redis();
$Redis->pconnect('127.0.0.1', 6379); // host, port
$Redis->select(15);  // switch to DB X

// Delete all cookies
GoodCookieDeleteAll($Redis);
?>

Display debug informations:
<?php
// Redis connection
$Redis = new Redis();
$Redis->pconnect('127.0.0.1', 6379); // host, port
$Redis->select(15);  // switch to DB X

// Display debug informations
echo GoodCookieDebug($Redis);
?>
Last update of this page: March 27, 2017
Please do not copy any of the content of this website without asking me first.