PHP Cookies Operation class (with source code)

Source: Internet
Author: User
Tags set cookie setcookie unpack
  1. /** Cookies class to save, read, update, and erase cookies data. Prefix can be set. Force timeout. The data can be strings, arrays, objects, and so on.
  2. * Date:2013-12-22
  3. * Author:fdipzone
  4. * ver:1.0
  5. * edit:bbs.it-home.org
  6. * Func:
  7. * Public Set Cookie
  8. * Public Get Read Cookie
  9. * Public Update Update Cookie
  10. * Public Clear Clear cookies
  11. * Public setprefix setting prefix
  12. * Public Setexpire Set Expiration time
  13. * Private Authcode encryption/decryption
  14. * Private Pack to package data
  15. * Private Unpack data unpacking
  16. * Private GetName Get cookie name, increase prefix processing
  17. */
  18. Class cookies{//Class start
  19. Private $_prefix = '; Cookie Prefix
  20. Private $_securekey = ' Ekot4_ut0f3xe-fjcpbvrfrg506jpcujeixezgpnyalm '; Encrypt key
  21. Private $_expire = 3600; Default expire
  22. /** initialization
  23. * @param String $prefix Cookie Prefix
  24. * @param int $expire Expiration Time
  25. * @param String $securekey Cookie Secure key
  26. */
  27. Public function __construct ($prefix = ', $expire =0, $securekey = ') {
  28. if (is_string ($prefix) && $prefix! = ") {
  29. $this->_prefix = $prefix;
  30. }
  31. if (Is_numeric ($expire) && $expire >0) {
  32. $this->_expire = $expire;
  33. }
  34. if (is_string ($securekey) && $securekey! = ") {
  35. $this->_securekey = $securekey;
  36. }
  37. }
  38. /** Setting cookies
  39. * @param String $name cookie Name
  40. * @param mixed $value cookie value can be a string, array, object, etc.
  41. * @param int $expire Expiration Time
  42. */
  43. Public function set ($name, $value, $expire =0) {
  44. $cookie _name = $this->getname ($name);
  45. $cookie _expire = time () + ($expire? $expire: $this->_expire);
  46. $cookie _value = $this->pack ($value, $cookie _expire);
  47. $cookie _value = $this->authcode ($cookie _value, ' ENCODE ', $this->_securekey);
  48. if ($cookie _name && $cookie _value && $cookie _expire) {
  49. Setcookie ($cookie _name, $cookie _value, $cookie _expire);
  50. }
  51. }
  52. /** reading Cookies
  53. * @param String $name cookie Name
  54. * @return Mixed Cookie value
  55. */
  56. Public function Get ($name) {
  57. $cookie _name = $this->getname ($name);
  58. if (Isset ($_cookie[$cookie _name])) {
  59. $cookie _value = $this->authcode ($_cookie[$cookie _name], ' DECODE ', $this->_securekey);
  60. $cookie _value = $this->unpack ($cookie _value);
  61. return Isset ($cookie _value[0])? $cookie _value[0]: null;
  62. }else{
  63. return null;
  64. }
  65. }
  66. /** Update cookies to update content only, use the Set method if you need to update the expiration time
  67. * @param String $name cookie Name
  68. * @param mixed $value cookie value
  69. * @return Boolean
  70. */
  71. Public Function Update ($name, $value) {
  72. $cookie _name = $this->getname ($name);
  73. if (Isset ($_cookie[$cookie _name])) {
  74. $old _cookie_value = $this->authcode ($_cookie[$cookie _name], ' DECODE ', $this->_securekey);
  75. $old _cookie_value = $this->unpack ($old _cookie_value);
  76. if (Isset ($old _cookie_value[1]) && $old _cookie_vlaue[1]>0) {//Get previous expiration time
  77. $cookie _expire = $old _cookie_value[1];
  78. Update data
  79. $cookie _value = $this->pack ($value, $cookie _expire);
  80. $cookie _value = $this->authcode ($cookie _value, ' ENCODE ', $this->_securekey);
  81. if ($cookie _name && $cookie _value && $cookie _expire) {
  82. Setcookie ($cookie _name, $cookie _value, $cookie _expire);
  83. return true;
  84. }
  85. }
  86. }
  87. return false;
  88. }
  89. /** Clearing Cookies
  90. * @param String $name cookie Name
  91. */
  92. Public function Clear ($name) {
  93. $cookie _name = $this->getname ($name);
  94. Setcookie ($cookie _name);
  95. }
  96. /** setting prefixes
  97. * @param String $prefix Cookie Prefix
  98. */
  99. Public Function Setprefix ($prefix) {
  100. if (is_string ($prefix) && $prefix! = ") {
  101. $this->_prefix = $prefix;
  102. }
  103. }
  104. /** Setting the Expiration time
  105. * @param int $expire cookie expire
  106. */
  107. Public Function Setexpire ($expire) {
  108. if (Is_numeric ($expire) && $expire >0) {
  109. $this->_expire = $expire;
  110. }
  111. }
  112. /** Get Cookie Name
  113. * @param String $name
  114. * @return String
  115. */
  116. Private Function GetName ($name) {
  117. Return $this->_prefix? $this->_prefix. ' _ '. $name: $name;
  118. }
  119. /** Pack
  120. * @param Mixed $data data
  121. * @param int $expire expiration Time to determine
  122. * @return
  123. */
  124. Private Function Pack ($data, $expire) {
  125. if ($data = = =) {
  126. Return ';
  127. }
  128. $cookie _data = Array ();
  129. $cookie _data[' value '] = $data;
  130. $cookie _data[' expire ') = $expire;
  131. Return Json_encode ($cookie _data);
  132. }
  133. /** Unpack
  134. * @param Mixed $data data
  135. * @return Array (data, expiration time)
  136. */
  137. Private Function Unpack ($data) {
  138. if ($data = = =) {
  139. Return Array (' ', 0);
  140. }
  141. $cookie _data = Json_decode ($data, true);
  142. if (isset ($cookie _data[' value ') && isset ($cookie _data[' expire '])) {
  143. if (Time () < $cookie _data[' expire ')} {//Not expired
  144. Return Array ($cookie _data[' value '], $cookie _data[' expire ');
  145. }
  146. }
  147. Return Array (' ', 0);
  148. }
  149. /** Encrypt/Decrypt data
  150. * @param String $str original text or ciphertext
  151. * @param String $operation ENCODE or DECODE
  152. * @return String returns plaintext ciphertext based on settings
  153. */
  154. Private Function Authcode ($string, $operation = ' DECODE ') {
  155. $ckey _length = 4; Random key length value 0-32;
  156. $key = $this->_securekey;
  157. $key = MD5 ($key);
  158. $keya = MD5 (substr ($key, 0, 16));
  159. $KEYB = MD5 (substr ($key, 16, 16));
  160. $KEYC = $ckey _length? ($operation = = ' DECODE '? substr ($string, 0, $ckey _length): substr (MD5 (Microtime ()),-$ckey _length)): ";
  161. $cryptkey = $keya. MD5 ($keya. $KEYC);
  162. $key _length = strlen ($cryptkey);
  163. $string = $operation = = = ' DECODE '? Base64_decode (substr ($string, $ckey _length)): sprintf ('%010d ', 0). substr (MD5 ($string. $keyb), 0, +). $string;
  164. $string _length = strlen ($string);
  165. $result = ";
  166. $box = Range (0, 255);
  167. $rndkey = Array ();
  168. for ($i = 0; $i <= 255; $i + +) {
  169. $rndkey [$i] = Ord ($cryptkey [$i% $key _length]);
  170. }
  171. for ($j = $i = 0; $i < $i + +) {
  172. $j = ($j + $box [$i] + $rndkey [$i])% 256;
  173. $tmp = $box [$i];
  174. $box [$i] = $box [$j];
  175. $box [$j] = $tmp;
  176. }
  177. for ($a = $j = $i = 0; $i < $string _length; $i + +) {
  178. $a = ($a + 1)% 256;
  179. $j = ($j + $box [$a])% 256;
  180. $tmp = $box [$a];
  181. $box [$a] = $box [$j];
  182. $box [$j] = $tmp;
  183. $result. = Chr (ord ($string [$i]) ^ ($box [($box [$a] + $box [$j])% 256]));
  184. }
  185. if ($operation = = ' DECODE ') {
  186. if (substr ($result, 0, ten) = = 0 | | substr ($result, 0,)-time () > 0) && substr ($result, ten, +) = = SUBSTR (MD 5 (substr ($result, +). $keyb), 0, 16)) {
  187. Return substr ($result, 26);
  188. } else {
  189. Return ';
  190. }
  191. } else {
  192. Return $KEYC. Str_replace (' = ', ' ', Base64_encode ($result));
  193. }
  194. }
  195. }//Class end
  196. ?>
Copy Code

2, demo example demo.php

  1. Require ' Cookies.class.php ';
  2. $type = isset ($_get[' type ')? Strtolower ($_get[' type '): ';
  3. if (!in_array ($type, Array (' Set ', ' Get ', ' Update ', ' Clear '))) {
  4. Exit (' type not exists ');
  5. }
  6. $obj = new Cookie (' member ', 10); Obj
  7. Switch ($type) {
  8. Case ' Set '://Set
  9. $data = Array (
  10. ' Name ' = ' Fdipzone ',
  11. ' Gender ' = ' male '
  12. );
  13. $obj->set (' Me ', $data, 5);
  14. echo ' Set cookies ';
  15. Break
  16. Case ' get '://Read
  17. $result = $obj->get (' Me ');
  18. Echo '
    ';  
  19. Print_r ($result);
  20. Echo '
  21. ';
  22. echo ' Get cookies ';
  23. Break
  24. Case ' Update '://Update
  25. $data = Array (
  26. ' Name ' = ' Angelababy ',
  27. ' Gender ' = ' female '
  28. );
  29. $flag = $obj->update (' Me ', $data);
  30. if ($flag) {
  31. Echo ' Update cookie success ';
  32. }else{
  33. echo ' Update cookie false ';
  34. }
  35. Break
  36. Case ' clear '://clear
  37. $obj->clear (' Me ');
  38. echo ' Clear cookies ';
  39. Break
  40. }
  41. ?>
Copy Code

Attached, the source code download address of the PHP cookie operation class

  • 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.