Cookie management using Cookie:: class.
authorFlorent Bruneau <florent.bruneau@polytechnique.org>
Sun, 21 Dec 2008 18:05:13 +0000 (19:05 +0100)
committerFlorent Bruneau <florent.bruneau@polytechnique.org>
Sun, 21 Dec 2008 18:05:13 +0000 (19:05 +0100)
 * Cookie::set() to set a cookie for a given number of days.
 * Cookie::kill() to kill a cookie.

Both methods change the value of the cookie both remotely (cookie stored by
the browser) and locally ($_COOKIE and Cookie::).

Signed-off-by: Florent Bruneau <florent.bruneau@polytechnique.org>
classes/env.php
classes/plglobals.php

index 61cc6cf..5b6a2b9 100644 (file)
@@ -197,19 +197,33 @@ class Cookie
 {
     public static function _get($key, $default)
     {
+        global $globals;
+        $key = $globals->cookie_ns . $key;
         return isset($_COOKIE[$key]) ? $_COOKIE[$key] : $default;
     }
 
     public static function has($key)
     {
+        global $globals;
+        $key = $globals->cookie_ns . $key;
         return isset($_COOKIE[$key]);
     }
 
     public static function kill($key)
     {
+        global $globals;
+        $key = $globals->cookie_ns . $key;
+        setcookie($key, '', time() - 3600, $globals->cookie_path);
         unset($_COOKIE[$key]);
     }
 
+    public static function set($key, $value, $days) {
+        global $globals;
+        $key = $globals->cookie_ns . $key;
+        setcookie($key, $value, time() + 86400 * $days, $globals->cookie_path);
+        $_COOKIE[$key] = $value;
+    }
+
     public static function v($key, $default = null)
     {
         return Cookie::_get($key, $default);
index ab50d9f..686afd5 100644 (file)
@@ -87,6 +87,11 @@ class PlGlobals
     public $locale;
     public $timezone;
 
+    /** Cookie configuration.
+     */
+    public $cookie_ns = 'ORG';
+    public $cookie_path = '/';
+
     /** You must give a list of file to load.
      * The filenames given are relatives to the config path of your plat/al installation.
      */