Cookie.class.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2012 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. // $Id: Cookie.class.php 2702 2012-02-02 12:35:01Z liu21st $
  12. /**
  13. +------------------------------------------------------------------------------
  14. * Cookie管理类
  15. +------------------------------------------------------------------------------
  16. * @category Think
  17. * @package Think
  18. * @subpackage Util
  19. * @author liu21st <liu21st@gmail.com>
  20. * @version $Id: Cookie.class.php 2702 2012-02-02 12:35:01Z liu21st $
  21. +------------------------------------------------------------------------------
  22. */
  23. class Cookie {
  24. // 判断Cookie是否存在
  25. static function is_set($name) {
  26. return isset($_COOKIE[C('COOKIE_PREFIX').$name]);
  27. }
  28. // 获取某个Cookie值
  29. static function get($name) {
  30. $value = $_COOKIE[C('COOKIE_PREFIX').$name];
  31. $value = unserialize(base64_decode($value));
  32. return $value;
  33. }
  34. // 设置某个Cookie值
  35. static function set($name,$value,$expire='',$path='',$domain='') {
  36. if($expire=='') {
  37. $expire = C('COOKIE_EXPIRE');
  38. }
  39. if(empty($path)) {
  40. $path = C('COOKIE_PATH');
  41. }
  42. if(empty($domain)) {
  43. $domain = C('COOKIE_DOMAIN');
  44. }
  45. $expire = !empty($expire)? time()+$expire : 0;
  46. $value = base64_encode(serialize($value));
  47. setcookie(C('COOKIE_PREFIX').$name, $value,$expire,$path,$domain);
  48. $_COOKIE[C('COOKIE_PREFIX').$name] = $value;
  49. }
  50. // 删除某个Cookie值
  51. static function delete($name) {
  52. Cookie::set($name,'',-3600);
  53. unset($_COOKIE[C('COOKIE_PREFIX').$name]);
  54. }
  55. // 清空Cookie值
  56. static function clear() {
  57. unset($_COOKIE);
  58. }
  59. }