1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- <?php
- /*************************************************************************|
- |--------------- Cookie class --------------------------------------------|
- |*************************************************************************|
-
- This class handles cookies.
-
- $Cookie->get(); is user provided and untrustworthy
-
- |*************************************************************************/
-
- /*
- interface COOKIE_INTERFACE {
- public function get($Key);
- public function set($Key, $Value, $Seconds, $LimitAccess);
- public function del($Key);
-
- public function flush();
- }
- */
-
- class COOKIE /*implements COOKIE_INTERFACE*/
- {
- const LIMIT_ACCESS = true; //If true, blocks JS cookie API access by default (can be overridden case by case)
- const PREFIX = ''; //In some cases you may desire to prefix your cookies
-
- public function get($Key)
- {
- if (!isset($_COOKIE[self::PREFIX.$Key])) {
- return false;
- }
- return $_COOKIE[self::PREFIX.$Key];
- }
-
- //Pass the 4th optional param as false to allow JS access to the cookie
- public function set($Key, $Value, $Seconds = 86400, $LimitAccess = self::LIMIT_ACCESS)
- {
- setcookie(self::PREFIX.$Key, $Value, time() + $Seconds, '/', SITE_DOMAIN, $_SERVER['SERVER_PORT'] === '443', $LimitAccess, false);
- }
-
- public function del($Key)
- {
- setcookie(self::PREFIX.$Key, '', time() - 24 * 3600); //3600 vs 1 second to account for potential clock desyncs
- }
-
- public function flush()
- {
- $Cookies = array_keys($_COOKIE);
- foreach ($Cookies as $Cookie) {
- $this->del($Cookie);
- }
- }
- }
|