A php class for processing cookies

Source: Internet
Author: User
A php class for processing cookies

  1. Login
  2. Register
  3. Subscribe to RSS
  4. Website map
  5. Script sharing network
  6. // CookieClass
  7. // Copyright (C) 2005 JRSofty Programming.
  8. // Http://jrsofty1.stinkbugonline.com
  9. // Licensed under GNU/GPL
  10. Class cookieClass {
  11. Var $ cName = '';
  12. Var $ cTime = '';
  13. Var $ cSerialize = false;
  14. Var $ cPath = '';
  15. Function cookieClass ($ cookieName, $ cookieTimeout, $ cookieSerialize = false, $ cookiePath = "/"){
  16. $ This-> cName = $ cookieName;
  17. $ This-> cTime = $ cookieTimeout;
  18. $ This-> cSerialize = $ cookieSerialize;
  19. $ This-> cPath = $ cookiePath;
  20. // This shoshould fix the issue if you have cookies set and THEN turn on the serialization.
  21. $ Iname = $ this-> cName. "_ S ";
  22. If ($ this-> cSerialize &&! Isset ($ _ COOKIE [$ iname]) {
  23. $ CookArr = array ();
  24. Foreach ($ _ COOKIE as $ name => $ val ){
  25. If (strpos ($ name, $ this-> cName )! = False) {// make sure it is a cookie set by this application
  26. $ Subname = substr ($ name, strlen ($ this-> cName) + 1 );
  27. $ CookArr [$ subname] = $ val;
  28. $ This-> KillCookie ($ name );
  29. }
  30. }
  31. $ This-> WriteCookie ($ cookArr );
  32. }
  33. // This is the opposite from above. changes a serialized cookie to multiple cookies without loss of data
  34. If (! $ This-> cSerialize & isset ($ _ COOKIE [$ iname]) {
  35. $ CookArr = unserialize ($ _ COOKIE [$ iname]);
  36. $ This-> KillCookie ($ iname );
  37. $ This-> WriteCookie ($ cookArr );
  38. }
  39. }
  40. Function DestroyAllCookies (){
  41. Foreach ($ _ COOKIE as $ name => $ val ){
  42. If (strpos ($ name, $ this-> cName )! = False ){
  43. $ _ COOKIE [$ name] = NULL;
  44. $ This-> KillCookie ($ name );
  45. }
  46. }
  47. }
  48. Function ReadCookie ($ item ){
  49. If ($ this-> cSerialize ){
  50. $ Name = $ this-> cName. "_ S ";
  51. If (isset ($ _ COOKIE [$ name]) {
  52. // Handle the cookie as a serialzied variable
  53. $ SCookie = unserialize ($ _ COOKIE [$ name]);
  54. If (isset ($ sCookie [$ item]) {
  55. Return $ sCookie [$ item];
  56. } Else {
  57. Return NULL;
  58. }
  59. } Else {
  60. Return NULL;
  61. }
  62. } Else {
  63. $ Name = $ this-> cName. "_". $ item;
  64. If (isset ($ _ COOKIE [$ name]) {
  65. // Handle the item as separate cookies
  66. Return $ _ COOKIE [$ name];
  67. } Else {
  68. Return NULL;
  69. }
  70. }
  71. }
  72. Function KillCookie ($ cName ){
  73. $ TStamp = time ()-432000;
  74. Setcookie ($ cName, "", $ tStamp, $ this-> cPath );
  75. }
  76. Function WriteCookie ($ itemArr ){
  77. If ($ this-> cSerialize ){
  78. $ SItems = serialize ($ itemArr );
  79. $ Name = $ this-> cName. "_ S ";
  80. $ _ COOKIE [$ name] = $ sItems;
  81. $ TStamp = time () + $ this-> cTime;
  82. Setcookie ($ name, $ sItems, $ tStamp, $ this-> cPath );
  83. } Else {
  84. $ TStamp = time () + $ this-> cTime;
  85. Foreach ($ itemArr as $ nam => $ val ){
  86. $ Name = $ this-> cName. "_". $ nam;
  87. $ _ COOKIE [$ name] = $ val;
  88. Setcookie ($ name, $ val, $ tStamp, $ this-> cPath );
  89. } E
  90. }
  91. }
  92. }
  93. ?>


Cookie, php

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.