使用法の例:
$eC = 新しいエンコードクッキー;
$e = $eC->encodeC ( md5 ('パスワード') );
$d = $eC->decodeC ( $e );
echo "元の Cookie 値: ".$d;
エコー「
」;
echo "エンコードされた Cookie 値: ".$e;
- define ("DOMAIN", "54dev.com");
- define ("PATH", "/");
- define ("COOKIEID", "encodeCookie");
- define ("COOKIEKEY", "raz "); // 最大 5 文字が適切です
-
- /**
- * class encodeCookie
- *
- * Cookie を送信する前にエンコードします
- *
- */
- class encodeCookie {
- /**
- * encodeCookie::$config
- *
- * 設定
- *
- */
- var $config;
-
- /**
- * encodeCookie::encodeCookie()
- *
- * コンストラクター
- *
- */
- function encodeCookie () {
- $this->config = array ();
- $this->config['cookie_key'] = COOKIEKEY;
- $this->config['cookie'] = array (
- 'cookie_id' => COOKIEID,
- 'cookie_path' => PATH,
- 'cookie_domain' => DOMAIN,
- );
- }
-
- /**
- * encodeCookie::set_Cookie()
- *
- * Cookie を設定します
- *
- * @param string $value
- * @param integer $sticky
- */
- function set_Cookie ($name, $value = "", $sticky = 0) {
-
- $exipres = "";
-
- if ($sticky == 1) {
- $expires = time() + 60*60*24*365;
- }
-
- $name = $this-> ;config['cookie']['cookie_id'].$name;
- $newValue = $this->encodeC ($value);
-
- @setcookie($name, urlencode($newValue), $expires, $this ->config['cookie']['cookie_path'], $this->config['cookie']['cookie_domain']);
- }
-
- /**
- * encodeCookie::get_Cookie()
- *
- * Cookie を取得します
- *
- */
- function get_Cookie ($ name) {
-
- if ( isset( $_COOKIE[$this->config['cookie']['cookie_id'].$name] ) ) {
- $cookie = urldecode ( $_COOKIE[$this->config ['cookie']['cookie_id'].$name] );
- return $this->decodeC ($cookie);
- } else {
- return FALSE;
- }
-
- }
-
- /**
- * encodeCookie::encodeC()
- *
- * Cookie をエンコードします
- *
- */
- function encodeC ($cookie) {
-
- $newcookie = array ();
- $cookie = Base64_encode ($cookie);
-
- for ( $i=0; $i $newcookie[ $i ] = ord ( $cookie[ $i ] ) * $this->encodeKey ();
- }
-
- $newcookie = implode ('.', $newcookie);
-
- return $newcookie;
- }
-
- /**
- * encodeCookie::decodeC()
- *
- * Cookie をデコードします
- *
- */
- function decodeC ($oldcookie) {
-
- $newcookie = array ();
- $cookie =explode ('.', $oldcookie);
-
- for ( $i=0; $i $newcookie[ $i ] = chr ( $cookie[ $i ] / $this->encodeKey () );
- }
-
- $newcookie = implode ('', $newcookie);
- $newcookie = Base64_decode ($newcookie);
-
- return $newcookie;
- }
-
- /**
- * encodeCookie::encodeKey()
- *
- * キーをエンコードします
- *
- */
- function encodeKey () {
- $newkey = 0;
- for ( $i=0; $i<=strlen ( $this->config['cookie_key'] ); $i++ ) {
- $newkey += ord ( $this->config['cookie_key '][ $i ] );
- }
- return $newkey;
- }
-
- }
复制代
|