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.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Cookie
  5. *
  6. * This class handles cookies.
  7. * $Cookie->get() is user-provided and untrustworthy.
  8. */
  9. class COOKIE
  10. {
  11. # In some cases you may desire to prefix your cookies
  12. const PREFIX = '';
  13. public function get($Key)
  14. {
  15. return (!isset($_COOKIE[SELF::PREFIX.$Key]))
  16. ? false
  17. : $_COOKIE[SELF::PREFIX.$Key];
  18. }
  19. // Pass the 4th optional param as false to allow JS access to the cookie
  20. public function set($Key, $Value, $Seconds = 86400)
  21. {
  22. $ENV = ENV::go();
  23. setcookie(
  24. SELF::PREFIX.$Key,
  25. $Value,
  26. [
  27. 'expires' => time() + $Seconds,
  28. 'path' => '/',
  29. 'domain' => $ENV->SITE_DOMAIN,
  30. 'secure' => true,
  31. 'httponly' => true,
  32. 'samesite' => 'Strict',
  33. ]
  34. );
  35. }
  36. public function del($Key)
  37. {
  38. # 3600s vs. 1s for potential clock desyncs
  39. setcookie(
  40. SELF::PREFIX.$Key,
  41. '',
  42. [
  43. 'expires' => time() - 24 * 3600,
  44. 'secure' => true,
  45. 'httponly' => true,
  46. 'samesite' => 'Strict',
  47. ]
  48. );
  49. }
  50. public function flush()
  51. {
  52. $Cookies = array_keys($_COOKIE);
  53. foreach ($Cookies as $Cookie) {
  54. $this->del($Cookie);
  55. }
  56. }
  57. }