当前位置:Gxlcms > PHP教程 > phpcookie类(用到了命名空间)

phpcookie类(用到了命名空间)

时间:2021-07-01 10:21:17 帮助过:16人阅读

分享一个php cookie操作类,用到了php中的命名空间,这个比较新颖,有需要的朋友参考下吧。

一个php cookie操作类,实现了基本的操作功能: 创建cookie、设置cookie的过期时间、注销cookie等。

代码:

  1. <!--?php
  2. namespace com\net {
  3. /**
  4. * php cookie操作类
  5. * by bbs.it-home.org
  6. */
  7. class Cookie {
  8. /**
  9. * cookie名称
  10. * @var string
  11. */
  12. public $name;
  13. /**
  14. * cookie值
  15. * @var string
  16. */
  17. public $value;
  18. /**
  19. * cookie过期时间
  20. * @var int
  21. */
  22. public $expire;
  23. /**
  24. * cookie 保存路径
  25. * @var string
  26. */
  27. public $path;
  28. /**
  29. * cookie域
  30. * @var string
  31. */
  32. public $domain;
  33. /**
  34. * Indicates that the cookie should only be transmitted over a secure HTTPS connection from the client.
  35. * When set to TRUE, the cookie will only be set if a secure connection exists.
  36. * On the server-side, it's on the programmer to send this kind of cookie only on secure connection
  37. * (e.g. with respect to $_SERVER["HTTPS"]).
  38. * @var boolean
  39. */
  40. public $secure;
  41. /**
  42. * When TRUE the cookie will be made accessible only through the HTTP protocol.
  43. * This means that the cookie won't be accessible by scripting languages, such as JavaScript.
  44. * This setting can effectively help to reduce identity theft through XSS attacks
  45. * (although it is not supported by all browsers).
  46. * Added in PHP 5.2.0. TRUE or FALSE
  47. * @var boolean
  48. */
  49. public $httponly;
  50. /**
  51. * 创建cookie
  52. *
  53. * @param string $name The name of the cookie.
  54. * @param string $value [optional] The value of the cookie.
  55. * @param int $expire [optional] The time the cookie expires.
  56. * @param string $path [optional] The path on the server in which the cookie will be available on.
  57. * @param string $domain [optional] The domain that the cookie is available to.
  58. * @param boolean $secure [optional] Indicates that the cookie should only be transmitted over a secure HTTPS connection from the client.
  59. * @param boolean $httponly [optional] Indicates that the cookie will be made accessible only through the HTTP protocol
  60. */
  61. public function __construct($name, $value = null, $expire = null, $path = null, $domain = null, $secure = null, $httponly = null){
  62. if(($this--->name = (string) $name)){
  63. if(!is_null($value)){
  64. $this->value = (string) $value;
  65. $this->expire = $expire;
  66. $this->path = $path;
  67. $this->domain = $domain;
  68. $this->secure = $secure;
  69. $this->httponly = $httponly;
  70. } else {
  71. $this->value = $this->exists() ? $_COOKIE[$this->name] : '';
  72. }
  73. } else {
  74. throw new Exception("invalid cookie name");
  75. }
  76. }
  77. /**
  78. * 检测cookie是否存在
  79. * @return boolean
  80. */
  81. public function exists(){
  82. return isset($_COOKIE[$this->name]);
  83. }
  84. /**
  85. * 通过setcookie设置cookie信息
  86. */
  87. public function save(){
  88. return setcookie($this->name, $this->value, $this->expire, $this->path, $this->domain, $this->secure, $this->httponly);
  89. }
  90. /**
  91. * 注销cookie
  92. */
  93. public function delete(){
  94. return setcookie($this->name, "", time() - 3600);
  95. }
  96. }
  97. }
  98. ?>

人气教程排行