-
-
/**
- --- 建立COOKIE 物件---
- $c = new cookie();
-
- --- 建立/更新COOKIE ---
- $c->setName(' myCookie ') // 必需
- ->setValue($value,true) // 必需- 第一個參數= 資料字串/數組,第二個參數= 加密(true=yes)
- ->setExpire( '+ 1 小時') // 可選- 預設為「0」或瀏覽器關閉
- ->setPath('/') // 可選- 預設為/
- ->setDomain('.domain. com') // 可選- 將嘗試自動偵測
- ->setSecure(false) // 可選- 預設false
- ->setHTTPOnly(false); // 可選- 預設false
- $c ->createCookie(); // 你也可以連結這個,必須是最後一個
-
- --- DESTROY COOKIE ---
- $c->setName('myCookie')->destroyCookie() ;
- 或
- $c->destroyCookie('myCookie');
-
- --- 取得COOKIE ---
- $cookie = $c->getCookie('myCookie',true ); // 第一個參數= cookie 名稱,第二個參數= 是否解密
- // 如果值為數組,則需要反序列化回傳
;*/
- Class Cookie {
- // cookie加密鍵值串,可以依自己的需要修改
- const DES_KEY = 'o89L7234kjW2Wad72SHw22lPZmEbP3dSj7TT10A5Sh60';
私有私有私有私有私有私有私什 = cookieData = null;
- 私有$cookieKey = null;
- 私有$cookieExpire = 0;
- 私有$cookiePath = '/';
- 私有$cookieDomain = null;
- 私有$se ;
- 私有$cookieHTTPOnly = false;
-
- /**
- * 建構子 設定域
- * @access public
- * @return none
- */
- public function __construct()
- {
- $this->cookieDomain = $ ();
- }
-
- /**
- * 取得cookie值
- * @access public
- * @param string $cookieName 要擷取的cookie
- * @param bool $decrypt 是否解密值
- */
- 公用函數getCookie($cookieName=null, $decrypt=false)
- {
- if(is_null($ cookieName)){
- $cookieName = $this->cookieName;
- }
- if( isset($_COOKIE[$cookieName])){
- return ($decrypt?Encryption( $_COOKIE[$cookieName],true):base64_decode($_COOKIE[$cookieName]));
- } else {
- $this->pushError($cookieName.'找不到');
- return false ;
- }
- }
-
- /**
- * 建立 cookie
- * @access public
- * @return bool true/false
- */
- public function createCookie()
- {
- if(is_null($this->cookieName)){
- $this->pushError('Cookie name was null');
- return false;
- }
-
- $ret = setcookie(
- $this->cookieName,
- $this->cookieData,
- $this->cookieExpire,
- $this->cookiePath,
- $this->cookieDomain,
- $this->cookieSecure,
- $this->cookieHTTPOnly
- );
-
- return $ret ;
- }
-
- /**
- * 清除cookie
- * @access public
- * @param string $cookieName 要殺死
- * @return bool true/false
- */
- public function destroyCookie($cookieName=null)
- {
- {
- if(is_null($cookieName)){
- $cookieName = $this->cookieName;
- }
-
- $ret = setcookie(
- $cookieName,
- null, null, null,
- (time()-1),
- $this->cookiePath,
- $this->cookieDomain
- );
-
- return $ret;
- }
-
- /**
- * 設定 cookie 名稱
- * @access public
- * @param string $name cookie 名稱
- * @return mix obj 或 bool false
- */
- public function setName($name=null)
- {
- if(!is_null($name)){
- $this->cookieName = $name ;
- return $this;
- }
- $this->pushError('Cookie 名稱為空');
- return false;
- }
-
- /**
- * 設定 cookie 值
- * @access public
- * @param string $value cookie 值
- * @return bool 字串是否為字串
- */
- public function setValue($value=null, $encrypt=false)
- {
- if(!is_null($value)){
- if(is_array($value)){
- $value = serialize($value);
- }
- $data = ($encrypt?$this->cookieEncryption($value):base64_encode($ value ));
- $len = (function_exists('mb_strlen')?mb_strlen($data):strlen($data));
- if($len>4096){
- $this->; PushError('Cookie 資料超過 4kb');
- return false;
- }
- $this->cookieData = $data;
- unset($data);
- return $this
- unset($data);
- return $this }
- $this->pushError('Cookie 值為空');
- return false;
- }
-
- /**
- * 設定cookie的過期時間
- * @access public
- * @param string $time +1 week, etc.
- * @return bool whether the string was a string
- */
- 公用函數setExpire( $time=0)
- {
- $pre = substr($time,0,1);
- if(in_array($pre, array('+','-'))){
- $this->cookieExpire = strtotime($time);
- return $this;
- } else {
- $this->cookieExpire = 0;
- return $this;
- }
- }
-
- /**
- * 設定cookie的儲存路徑
- * @access public
- * @param string $path
- * @return object $this
- */
- public function setPath($path='/')
- {
- $this->cookiePath = $path ;
- return $this;
- }
-
- /**
- * 設定cookie所屬的域
- * @access public
- * @param string $domain
- * @return object $this
- */
- public function setDomain($domain=null)
- {
- if(!is_null( $domain)){
- $this->cookieDomain = $domain;
- }
- return $this;
- }
-
- /**
- *
- * @access public
- * @param bool $secure true/false
- * @return object $this
- */
- public function setSecure($secure =false)
- {
- $this->cookieSecure = (bool)$secure;
- return $this;
- }
-
- /**
- * HTTPOnly 標誌,尚未被所有瀏覽器完全支援
- * @access public
- * @param bool $httponly yes/no
- * @return object $this
- */
- public function setHTTPOnly($httponly=false)
- {
- $this->cookieHTTPOnly = (bool)$httponly;
- return $this;
- }
-
- /***/
- 導管函數getRootDomain()
- {
- $host = $_SERVER['HTTP_HOST'];
- $parts =explode('.', $Host);
- if( count($parts)>1){
- $tld = array_pop($parts);
- $domain = array_pop($parts).'.'.$tld;
- } else {
- $網域= array_pop($parts);
- }
- return '.'.$domain;
- }
-
- /**
- * 如果未提供,則用於檢索根域的 Jenky 位元
- * @access private
- * @return string Le Domain
- */
- private function cookieEncryption($str =null, $decrypt=false)
- {
- if(is_null($str)){
- $this->pushError('無法加密/解密空字串');
- return $str ;
- }
$iv_size = mcrypt_get_iv_size(MCRYPT_3DES, MCRYPT_MODE_ECB);
- $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
- $key_size = mcrypt_get_key_size(MCRYPT_3DES, MCRYPT_MODE_ECB);
- $key = substr(self::DES_KEY,0,$key_size);
if($decrypt) > $return = mcrypt_decrypt(MCRYPT_3DES, $key, base64_decode($str), MCRYPT_MODE_ECB, $iv);
- } else {
- $return = base64_encode(mcrypt_encry, $ $iv));
- }
return $return;
- }
-
- /**
- * 值加密
- * @access private
- * @param string $str 待加密的字串
- * @param string $decrypt 是否解密
- * @返回字串(解密|加密)加密字串
- */
- 初始化函數PushError($error =null)
- {
- if(!is_null($error)) {
- $this->errors[] = $error;
- }
- return;
- }
-
- /**
- * 將錯誤加到錯誤數組
- * @access public
- * @param string $error
- * @return none
- */
- public function getErrors( )
- {
- return implode("
>", $this->errors);
- }
- }
-
複製程式碼
呼叫範例:
-
-
require('cookie.class.php');
// 樣本資料
- $array = array('foo'=>'bar','bar'=>'foo');
- $string = '這是一個字串';
$c = new Cookie();
/*
- 建立一個加密的cookie 資料庫
- */
- echo '
加密陣列';
$start = microtime(true);
$c->setName('Example') // 我們的cookie 名稱
- ->setValue($array,true) // 第二個參數,true,加密資料
- ->setExpire('+1 小時') // 1 小時後過期
- ->setPath(' /') // cookie 路徑
- ->setDomain('localhost') // 設定本地主機
- ->createCookie();
- $cookie = $c->getCookie('範例',true) ;
- $cookie = unserialize($cookie);
-
$bench = sprintf('%.8f',(microtime(true)-$start)); > ;
-
echo print_r($cookie,true).' '.$bench.'秒
';
/*
- 調查cookie
- */
- //$c->destroyCookie('Example');< ;/p>
/*
- 建立一個cookie字串,直接瀏覽器關閉時故障
- */
- echo '
常規未加密字串' ;
- $start = microtime(true);
- $c->setName('Example1')
- - >setValue($string) // 第二個參數可以設定為false
- ->setExpire(0)
- ->gt ;setPath('/')
- ->setDomain('localhost')
- ->createCookie();
$cookie = $c->getCookie ('範例1');
$bench = sprintf('% .8f',(microtime(true)-$start));
- echo print_r($cookie,true).'
'.$bench.'秒'; p>
-
複製程式碼
|