BioTorrents.de’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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. }