COOKIE encryption function

Source: Internet
Author: User
COOKIE encryption function
Example usage:
$ EC = new encodeCookie;
$ E = $ eC-> encodeC (md5 ('password'); $ d = $ eC-> decodeC ($ e );
Echo "Original Cookie value:". $ d; echo"

"; Echo" Encoded Cookie value: ". $ e;

  1. Define ("DOMAIN", "54dev.com ");
  2. Define ("PATH ","/");
  3. Define ("COOKIEID", "encodeCookie ");
  4. Define ("COOKIEKEY", "raz"); // max 5 chars is good
  5. /**
  6. * Class encodeCookie
  7. *
  8. * Encode cookies before you send them
  9. *
  10. */
  11. Class encodeCookie {
  12. /**
  13. * EncodeCookie: $ config
  14. *
  15. * Configuration
  16. *
  17. */
  18. Var $ config;
  19. /**
  20. * EncodeCookie: encodeCookie ()
  21. *
  22. * Constructor
  23. *
  24. */
  25. Function encodeCookie (){
  26. $ This-> config = array ();
  27. $ This-> config ['cookie _ key'] = COOKIEKEY;
  28. $ This-> config ['cooker'] = array (
  29. 'Cookie _ id' => COOKIEID,
  30. 'Cookie _ path' => path,
  31. 'Cookie _ domain '=> DOMAIN,
  32. );
  33. }
  34. /**
  35. * EncodeCookie: set_Cookie ()
  36. *
  37. * Sets the cookie
  38. *
  39. * @ Param string $ value
  40. * @ Param integer $ sticky
  41. */
  42. Function set_Cookie ($ name, $ value = "", $ sticky = 0 ){
  43. $ Exipres = "";
  44. If ($ sticky = 1 ){
  45. $ Expires = time () + 60x60x24*365;
  46. }
  47. $ Name = $ this-> config ['cookies'] ['cookie _ id']. $ name;
  48. $ NewValue = $ this-> encodeC ($ value );
  49. @ Setcookie ($ name, urlencode ($ newValue), $ expires, $ this-> config ['cookies'] ['cookie _ path'], $ this-> config ['cookies'] ['cookie _ domain ']);
  50. }
  51. /**
  52. * EncodeCookie: get_Cookie ()
  53. *
  54. * Gets the cookie
  55. *
  56. */
  57. Function get_Cookie ($ name ){
  58. If (isset ($ _ COOKIE [$ this-> config ['cookies'] ['cookie _ id']. $ name]) {
  59. $ Cookie = urldecode ($ _ COOKIE [$ this-> config ['cookier'] ['cookie _ id']. $ name]);
  60. Return $ this-> decodeC ($ cookie );
  61. } Else {
  62. Return FALSE;
  63. }
  64. }
  65. /**
  66. * EncodeCookie: encodeC ()
  67. *
  68. * Encodes the cookie
  69. *
  70. */
  71. Function encodeC ($ cookie ){
  72. $ Newcookie = array ();
  73. $ Cookie = base64_encode ($ cookie );
  74. For ($ I = 0; $ I <= strlen ($ cookie); $ I ++ ){
  75. $ Newcookie [$ I] = ord ($ cookie [$ I]) * $ this-> encodeKey ();
  76. }
  77. $ Newcookie = implode ('.', $ newcookie );
  78. Return $ newcookie;
  79. }
  80. /**
  81. * EncodeCookie: decodeC ()
  82. *
  83. * Decodes the cookie
  84. *
  85. */
  86. Function decodeC ($ oldcookie ){
  87. $ Newcookie = array ();
  88. $ Cookie = explode ('.', $ oldcookie );
  89. For ($ I = 0; $ I <= strlen ($ oldcookie); $ I ++ ){
  90. $ Newcookie [$ I] = chr ($ cookie [$ I]/$ this-> encodeKey ());
  91. }
  92. $ Newcookie = implode ('', $ newcookie );
  93. $ Newcookie = base64_decode ($ newcookie );
  94. Return $ newcookie;
  95. }
  96. /**
  97. * EncodeCookie: encodeKey ()
  98. *
  99. * Encodes the key
  100. *
  101. */
  102. Function encodeKey (){
  103. $ Newkey = 0;
  104. For ($ I = 0; $ I <= strlen ($ this-> config ['cookie _ key']); $ I ++ ){
  105. $ Newkey + = ord ($ this-> config ['cookie _ key'] [$ I]);
  106. }
  107. Return $ newkey;
  108. }
  109. }

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.