COOKIE 암호화 기능
풀어 주다: 2016-07-25 09:11:15
示例用法:
$eC = new encodeCookie;
$e = $eC->encodeC ( md5 ('password') );
$d = $eC->decodeC ( $e );
echo "Original Cookie value : ".$d;
echo "
";
echo "Encoded Cookie value : ".$e;
- define ("DOMAIN", "54dev.com");
- define ("PATH", "/");
- define ("COOKIEID", "encodeCookie");
- define ("COOKIEKEY", "raz"); // max 5 chars is good
-
- /**
- * class encodeCookie
- *
- * encode cookies before you send them
- *
- */
- class encodeCookie {
- /**
- * encodeCookie::$config
- *
- * configuration
- *
- */
- var $config;
-
- /**
- * encodeCookie::encodeCookie()
- *
- * constructor
- *
- */
- 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()
- *
- * sets the 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()
- *
- * gets the 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()
- *
- * encodes the cookie
- *
- */
- function encodeC ($cookie) {
-
- $newcookie = array ();
- $cookie = base64_encode ($cookie);
-
- for ( $i=0; $i<=strlen ($cookie); $i ) {
- $newcookie[ $i ] = ord ( $cookie[ $i ] ) * $this->encodeKey ();
- }
-
- $newcookie = implode ('.', $newcookie);
-
- return $newcookie;
- }
-
- /**
- * encodeCookie::decodeC()
- *
- * decodes the cookie
- *
- */
- function decodeC ($oldcookie) {
-
- $newcookie = array ();
- $cookie = explode ('.', $oldcookie);
-
- for ( $i=0; $i<=strlen ($oldcookie); $i ) {
- $newcookie[ $i ] = chr ( $cookie[ $i ] / $this->encodeKey () );
- }
-
- $newcookie = implode ('', $newcookie);
- $newcookie = base64_decode ($newcookie);
-
- return $newcookie;
- }
-
- /**
- * encodeCookie::encodeKey()
- *
- * encodes the key
- *
- */
- function encodeKey () {
- $newkey = 0;
- for ( $i=0; $i<=strlen ( $this->config['cookie_key'] ); $i ) {
- $newkey = ord ( $this->config['cookie_key'][ $i ] );
- }
- return $newkey;
- }
-
- }
复制代码
|
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
저자별 최신 기사
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31