Oppaitime's version of Gazelle
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cookie.class.php 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. /*************************************************************************|
  3. |--------------- Cookie class --------------------------------------------|
  4. |*************************************************************************|
  5. This class handles cookies.
  6. $Cookie->get(); is user provided and untrustworthy
  7. |*************************************************************************/
  8. /*
  9. interface COOKIE_INTERFACE {
  10. public function get($Key);
  11. public function set($Key, $Value, $Seconds, $LimitAccess);
  12. public function del($Key);
  13. public function flush();
  14. }
  15. */
  16. class COOKIE /*implements COOKIE_INTERFACE*/
  17. {
  18. const LIMIT_ACCESS = true; //If true, blocks JS cookie API access by default (can be overridden case by case)
  19. const PREFIX = ''; //In some cases you may desire to prefix your cookies
  20. public function get($Key)
  21. {
  22. if (!isset($_COOKIE[self::PREFIX.$Key])) {
  23. return false;
  24. }
  25. return $_COOKIE[self::PREFIX.$Key];
  26. }
  27. //Pass the 4th optional param as false to allow JS access to the cookie
  28. public function set($Key, $Value, $Seconds = 86400, $LimitAccess = self::LIMIT_ACCESS)
  29. {
  30. setcookie(self::PREFIX.$Key, $Value, time() + $Seconds, '/', SITE_DOMAIN, $_SERVER['SERVER_PORT'] === '443', $LimitAccess, false);
  31. }
  32. public function del($Key)
  33. {
  34. setcookie(self::PREFIX.$Key, '', time() - 24 * 3600); //3600 vs 1 second to account for potential clock desyncs
  35. }
  36. public function flush()
  37. {
  38. $Cookies = array_keys($_COOKIE);
  39. foreach ($Cookies as $Cookie) {
  40. $this->del($Cookie);
  41. }
  42. }
  43. }