Home >Backend Development >PHP Tutorial >COOKIE encryption function

COOKIE encryption function

WBOY
WBOYOriginal
2016-07-25 09:11:151000browse
示例用法:
$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['cookie'] = 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() + 60*60*24*365;
  46. }
  47. $name = $this->config['cookie']['cookie_id'].$name;
  48. $newValue = $this->encodeC ($value);
  49. @setcookie($name, urlencode($newValue), $expires, $this->config['cookie']['cookie_path'], $this->config['cookie']['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['cookie']['cookie_id'].$name] ) ) {
  59. $cookie = urldecode ( $_COOKIE[$this->config['cookie']['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. }
复制代码


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Another cache classNext article:Another cache class