首页 >后端开发 >php教程 >COOKIE加密函数

COOKIE加密函数

WBOY
WBOY原创
2016-07-25 09:11:151008浏览
示例用法:
$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 $newcookie[ $i ] = ord ( $cookie[ $i ] ) * $this->encodeKey ();
  75. }
  76. $newcookie = implode ('.', $newcookie);
  77. return $newcookie;
  78. }
  79. /**
  80. * encodeCookie::decodeC()
  81. *
  82. * decodes the cookie
  83. *
  84. */
  85. function decodeC ($oldcookie) {
  86. $newcookie = array ();
  87. $cookie = explode ('.', $oldcookie);
  88. for ( $i=0; $i $newcookie[ $i ] = chr ( $cookie[ $i ] / $this->encodeKey () );
  89. }
  90. $newcookie = implode ('', $newcookie);
  91. $newcookie = base64_decode ($newcookie);
  92. return $newcookie;
  93. }
  94. /**
  95. * encodeCookie::encodeKey()
  96. *
  97. * encodes the key
  98. *
  99. */
  100. function encodeKey () {
  101. $newkey = 0;
  102. for ( $i=0; $iconfig['cookie_key'] ); $i++ ) {
  103. $newkey += ord ( $this->config['cookie_key'][ $i ] );
  104. }
  105. return $newkey;
  106. }
  107. }
复制代码


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn