[PHP] php COOKIE加密函数 (按位求ascii编码 乘key) →→→→→进入此内容的聊天室

来自 , 2019-07-05, 写在 PHP, 查看 117 次.
URL http://www.code666.cn/view/be767243
  1. define ("DOMAIN", "54dev.com");
  2. define ("PATH", "/");
  3. define ("COOKIEID", "encodeCookie");
  4. define ("COOKIEKEY", "raz"); // max 5 chars is good
  5.  
  6. /**
  7. * class encodeCookie
  8. *
  9. * encode cookies before you send them
  10. *
  11. */
  12. class encodeCookie {
  13. /**
  14. * encodeCookie::$config
  15. *
  16. * configuration
  17. *
  18. */
  19.     var $config;
  20.  
  21. /**
  22. * encodeCookie::encodeCookie()
  23. *
  24. * constructor
  25. *
  26. */
  27. function encodeCookie () {
  28.     $this->config = array ();
  29.     $this->config['cookie_key'] = COOKIEKEY;
  30.     $this->config['cookie'] = array (
  31.                 'cookie_id'             =>   COOKIEID,
  32.                 'cookie_path'           =>   PATH,
  33.                 'cookie_domain'         =>   DOMAIN,
  34.     );
  35. }
  36.  
  37. /**
  38. * encodeCookie::set_Cookie()
  39. *
  40. * sets the cookie
  41. *
  42. * @param string $value
  43. * @param integer $sticky
  44. */
  45. function set_Cookie ($name, $value = "", $sticky = 0) {
  46.  
  47.     $exipres = "";
  48.  
  49.     if ($sticky == 1) {
  50.         $expires = time() + 60*60*24*365;
  51.     }
  52.  
  53.     $name = $this->config['cookie']['cookie_id'].$name;
  54.     $newValue = $this->encodeC ($value);
  55.  
  56.     @setcookie($name, urlencode($newValue), $expires, $this->config['cookie']['cookie_path'], $this->config['cookie']['cookie_domain']);
  57. }
  58.  
  59. /**
  60. * encodeCookie::get_Cookie()
  61. *
  62. * gets the cookie
  63. *
  64. */
  65. function get_Cookie ($name) {
  66.  
  67.     if ( isset( $_COOKIE[$this->config['cookie']['cookie_id'].$name] ) ) {
  68.         $cookie = urldecode ( $_COOKIE[$this->config['cookie']['cookie_id'].$name] );
  69.         return $this->decodeC ($cookie);
  70.     } else {
  71.         return FALSE;
  72.     }
  73.  
  74. }
  75.  
  76. /**
  77. * encodeCookie::encodeC()
  78. *
  79. * encodes the cookie
  80. *
  81. */
  82. function encodeC ($cookie) {
  83.  
  84.     $newcookie = array ();
  85.     $cookie = base64_encode ($cookie);
  86.  
  87.     for ( $i=0; $i<=strlen ($cookie); $i++ ) {
  88.         $newcookie[ $i ] = ord ( $cookie[ $i ] ) * $this->encodeKey ();
  89.     }
  90.  
  91.     $newcookie = implode ('.', $newcookie);
  92.  
  93. return $newcookie;
  94. }
  95.  
  96. /**
  97. * encodeCookie::decodeC()
  98. *
  99. * decodes the cookie
  100. *
  101. */
  102. function decodeC ($oldcookie) {
  103.  
  104.     $newcookie = array ();
  105.     $cookie = explode ('.', $oldcookie);
  106.  
  107.     for ( $i=0; $i<=strlen ($oldcookie); $i++ ) {
  108.         $newcookie[ $i ] = chr ( $cookie[ $i ] / $this->encodeKey () );
  109.     }
  110.  
  111.     $newcookie = implode ('', $newcookie);
  112.     $newcookie = base64_decode ($newcookie);
  113.  
  114. return $newcookie;
  115. }
  116.  
  117. /**
  118. * encodeCookie::encodeKey()
  119. *
  120. * encodes the key
  121. *
  122. */
  123. function encodeKey () {
  124.     $newkey = 0;
  125.     for ( $i=0; $i<=strlen ( $this->config['cookie_key'] ); $i++ ) {
  126.         $newkey += ord ( $this->config['cookie_key'][ $i ] );
  127.     }
  128. return $newkey;
  129. }
  130.  
  131. }
  132.  
  133.  

回复 "php COOKIE加密函数 (按位求ascii编码 乘key)"

这儿你可以回复上面这条便签

captcha