Cookie.class.php 2.2 KB

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