Advertisement
Guest User

Kyle Florence

a guest
Apr 30th, 2009
3,741
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.19 KB | None | 0 0
  1. <?php
  2. class Cookie
  3. {
  4.     // Reserved session keys
  5.     private static $_reserved = array();
  6.  
  7.     // Static class cannot be initialized
  8.     private function __construct() {}
  9.    
  10.     // Alias for delete() function
  11.     public static function del($key)
  12.     {
  13.         self::delete($key);
  14.     }
  15.    
  16.     // Delete a cookie
  17.     public static function delete($key)
  18.     {
  19.         // Change string representation array to key/value array
  20.         $key = self::_scrubKey($key);
  21.        
  22.         // Make sure the cookie exists
  23.         if (self::exists($key))
  24.         {          
  25.             // Check for key array
  26.             if (is_array($key))
  27.             {
  28.                 // Grab key/value pair
  29.                 list ($k, $v) = each($key);
  30.                
  31.                 // Set string representation
  32.                 $key = $k . '[' . $v . ']';
  33.                
  34.                 // Set expiration time to -1hr (will cause browser deletion)
  35.                 setcookie($key, false, time() - 3600);
  36.                
  37.                 // Unset the cookie
  38.                 unset($_COOKIE[$k][$v]);
  39.             }
  40.            
  41.             // Check for cookie array
  42.             else if (is_array($_COOKIE[$key]))
  43.             {
  44.                 foreach ($_COOKIE[$key] as $k => $v)
  45.                 {
  46.                     // Set string representation
  47.                     $cookie = $key . '[' . $k . ']';
  48.                    
  49.                     // Set expiration time to -1hr (will cause browser deletion)
  50.                     setcookie($cookie, false, time() - 3600);
  51.                    
  52.                     // Unset the cookie
  53.                     unset($_COOKIE[$key][$k]);
  54.                 }
  55.             }
  56.            
  57.             // Unset single cookie
  58.             else
  59.             {
  60.                 // Set expiration time to -1hr (will cause browser deletion)
  61.                 setcookie($key, false, time() - 3600);
  62.                
  63.                 // Unset key
  64.                 unset($_COOKIE[$key]);
  65.             }
  66.         }
  67.     }
  68.    
  69.     // See if a cookie key exists
  70.     public static function exists($key)
  71.     {
  72.         // Change string representation array to key/value array
  73.         $key = self::_scrubKey($key);
  74.        
  75.         // Check for array
  76.         if (is_array($key))
  77.         {
  78.             // Grab key/value pair
  79.             list ($k, $v) = each($key);
  80.            
  81.             // Check for key/value pair and return
  82.             if (isset($_COOKIE[$k][$v])) return true;
  83.         }
  84.        
  85.         // If key exists, return true
  86.         else if (isset($_COOKIE[$key])) return true;
  87.        
  88.         // Key does not exist
  89.         return false;
  90.     }
  91.    
  92.     // Get cookie information
  93.     public static function get($key)
  94.     {
  95.         // Change string representation array to key/value array
  96.         $key = self::_scrubKey($key);
  97.        
  98.         // Check for array
  99.         if (is_array($key))
  100.         {
  101.             // Grab key/value pair
  102.             list ($k, $v) = each($key);
  103.            
  104.             // Check for key/value pair and return
  105.             if (isset($_COOKIE[$k][$v])) return $_COOKIE[$k][$v];
  106.         }
  107.  
  108.         // Return single key if it's set
  109.         else if (isset($_COOKIE[$key])) return $_COOKIE[$key];
  110.            
  111.         // Otherwise return null
  112.         else return null;
  113.     }
  114.    
  115.     // Return the cookie array
  116.     public static function contents()
  117.     {
  118.         return $_COOKIE;
  119.     }
  120.    
  121.     // Set cookie information
  122.     public static function set(
  123.         $key,
  124.         $value,
  125.         $expire = 0,            /* Default expire time (session, 1 week = 604800) */
  126.         $path = '',             /* Default path */
  127.         $domain = '',           /* Default domain */
  128.         $secure = false,        /* Does this cookie need a secure HTTPS connection? */
  129.         $httponly = true        /* Can non-HTTP services access this cookie (IE: javascript)? */
  130.     ){      
  131.         // Make sure they aren't trying to set a reserved word
  132.         if (!in_array($key, self::$_reserved))
  133.         {      
  134.             // If $key is in array format, change it to string representation
  135.             $key = self::_scrubKey($key, true);
  136.                
  137.             // Store the cookie
  138.             setcookie($key, $value, $expire, $path, $domain, $secure, $httponly);  
  139.         }
  140.            
  141.         // Otherwise, throw an error
  142.         else Error::warning('Could not set key -- it is reserved.', __CLASS__);
  143.     }
  144.    
  145.     // Converts strings to arrays (or vice versa if toString = true)
  146.     private static function _scrubKey($key, $toString = false)
  147.     {
  148.         // Converting from array to string
  149.         if ($toString)
  150.         {
  151.             // If $key is in array format, change it to string representation
  152.             if (is_array($key))
  153.             {
  154.                 // Grab key/value pair
  155.                 list ($k, $v) = each($key);
  156.                
  157.                 // Set string representation
  158.                 $key = $k . '[' . $v . ']';
  159.             }
  160.         }
  161.        
  162.         // Converting from string to array
  163.         else if (!is_array($key))
  164.         {
  165.             // is this a string representation of an array?
  166.             if (preg_match('/([\w\d]+)\[([\w\d]+)\]$/i', $key, $matches))
  167.             {
  168.                 // Store as key/value pair
  169.                 $key = array($matches[1] => $matches[2]);
  170.             }
  171.         }
  172.        
  173.         // Return key
  174.         return $key;
  175.     }
  176. }
  177. ?>
  178.  
  179. @@Example usage:
  180.  
  181. <?php
  182. // Basic usage
  183. Cookie::set('testcookie', 'test');
  184. print(Cookie::get('testcookie'));
  185.  
  186. // You can set 'array' cookies in two different ways:
  187. Cookie::set('array[one]', 'item one');
  188. Cookie::set(array('array' => 'two'), 'item two');
  189.  
  190. // Likewise, you can also get 'array' cookies in two different ways:
  191. print(Cookie::get('array[one]'));
  192. print(Cookie::get(array('array' => 'one')));
  193.  
  194. // Or you can grab the whole array at once:
  195. print_r(Cookie::get('array'));
  196.  
  197. // Deleting cookies is done in the same way:
  198. Cookie::del('array[one]');
  199. Cookie::del(array('array' => 'two'));
  200.  
  201. // Delete the entire array:
  202. Cookie::del('array');
  203.  
  204. // Print contents of $_COOKIE (refresh for this)
  205. print '<pre>';
  206. print_r(Cookie::contents());
  207. print '<pre>';
  208. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement