PHP cookies (source code)

Source: Internet
Author: User
PHP cookies (source code)

  1. /** Cookies class: save, read, update, and clear cookies. You can set the prefix. Force timeout. Data can be strings, arrays, and objects.
  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 reads cookies.
  9. * Public update: update cookie
  10. * Public clear cookie
  11. * Set the public setPrefix.
  12. * Public setExpire: Set the Expiration Time
  13. * Private authcode encryption/decryption
  14. * Private pack data
  15. * Private unpack data
  16. * Private getName: Get the cookie name and add 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. /** Set cookie
  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, 'encoding', $ this-> _ securekey );
  48. If ($ cookie_name & $ cookie_value & $ cookie_expire ){
  49. Setcookie ($ cookie_name, $ cookie_value, $ cookie_expire );
  50. }
  51. }
  52. /** Read cookie
  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 cookie. only content is updated. use the set method 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) {// Obtain the 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, 'encoding', $ 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. /** Clear cookie
  90. * @ Param String $ name cookie name
  91. */
  92. Public function clear ($ name ){
  93. $ Cookie_name = $ this-> getName ($ name );
  94. Setcookie ($ cookie_name );
  95. }
  96. /** Set the prefix
  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. /** Set 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
  121. * @ Param int $ expire expiration time is used 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
  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 return the plaintext ciphertext based on the settings
  153. */
  154. Private function authcode ($ string, $ operation = 'decode '){
  155. $ Ckey_length = 4; // The random key length ranges from 0 to 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, 16 ). $ string;
  164. $ String_length = strlen ($ string );
  165. $ Result = '';
  166. $ Box = range (1, 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 <256; $ 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, 10) = 0 | substr ($ result, 0, 10)-time ()> 0) & substr ($ result, 10, 16) = substr (md5 (substr ($ result, 26 ). $ 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. ?>

2. 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 Cookies ('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 cookies success ';
  32. } Else {
  33. Echo 'update cookies false ';
  34. }
  35. Break;
  36. Case 'clear': // clear
  37. $ Obj-> clear ('me ');
  38. Echo 'clear cookies ';
  39. Break;
  40. }
  41. ?>

Appendix: PHP Cookies Operation Source code

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.