国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
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.

84 lines
2.7KB

  1. <?php
  2. if (!defined('DEDEINC')) exit ('dedebiz');
  3. /**
  4. * Cookie处理助手
  5. *
  6. * @version $id:cookie.helper.php 2024-04-11 tianya $
  7. * @package DedeBIZ.Helpers
  8. * @copyright Copyright (c) 2022 DedeBIZ.COM
  9. * @license GNU GPL v2 (https://www.dedebiz.com/license)
  10. * @link https://www.dedebiz.com
  11. */
  12. function DedeSetCookie($key, $value = "", $expires = 0, $path = ""){
  13. global $cfg_domain_cookie,$cfg_cookie_samesite,$cfg_cookie_secure,$cfg_cookie_httponly;
  14. if (version_compare(PHP_VERSION, '7.3.0', '>=')) {
  15. $options = array(
  16. "expires" => $expires,
  17. 'path' => $path,
  18. 'domain' => $cfg_domain_cookie,
  19. 'samesite' => $cfg_cookie_samesite,
  20. 'secure' => $cfg_cookie_secure,
  21. 'httponly' => $cfg_cookie_httponly,
  22. );
  23. setcookie($key, $value, $options);
  24. } else {
  25. $cookie_header = 'Set-Cookie: '.$key.'='.rawurlencode($value);
  26. $cookie_header .= ($expires === 0 ? '' : '; Expires='.gmdate('D, d-M-Y H:i:s T', $expires)).';';
  27. $cookie_header .= '; Path='.$path.($cfg_domain_cookie !== '' ? '; Domain='.$cfg_domain_cookie : '');
  28. $cookie_header .= ($cfg_cookie_secure ? '; Secure' : '').($cfg_cookie_httponly ? '; HttpOnly' : '').'; SameSite='.$cfg_cookie_samesite;
  29. header($cookie_header);
  30. return;
  31. }
  32. }
  33. /**
  34. * 设置Cookie记录
  35. *
  36. * @param string $key 键
  37. * @param string $value 值
  38. * @param string $kptime 保持时间
  39. * @param string $pa 保存路径
  40. * @return void
  41. */
  42. if (!function_exists('PutCookie')) {
  43. function PutCookie($key, $value, $kptime = 0, $pa = "/")
  44. {
  45. global $cfg_cookie_encode;
  46. DedeSetCookie($key, $value, time() + $kptime, $pa);
  47. DedeSetCookie($key.'__ckMd5', substr(md5($cfg_cookie_encode.$value), 0, 16), time() + $kptime, $pa);
  48. }
  49. }
  50. /**
  51. * 清除Cookie记录
  52. *
  53. * @param $key 键名
  54. * @return void
  55. */
  56. if (!function_exists('DropCookie')) {
  57. function DropCookie($key)
  58. {
  59. DedeSetCookie($key, '', time() - 360000, "/");
  60. DedeSetCookie($key.'__ckMd5', '', time() - 360000, "/");
  61. }
  62. }
  63. /**
  64. * 获取Cookie记录
  65. *
  66. * @param $key 键名
  67. * @return string
  68. */
  69. if (!function_exists('GetCookie')) {
  70. function GetCookie($key)
  71. {
  72. global $cfg_cookie_encode;
  73. if (!isset($_COOKIE[$key]) || !isset($_COOKIE[$key.'__ckMd5'])) {
  74. return '';
  75. } else {
  76. if ($_COOKIE[$key.'__ckMd5'] != substr(md5($cfg_cookie_encode.$_COOKIE[$key]), 0, 16)) {
  77. return '';
  78. } else {
  79. return $_COOKIE[$key];
  80. }
  81. }
  82. }
  83. }
  84. ?>