php實現cookie加密解密

來源:互聯網
上載者:User
1.加密解密類

 

 1 class Mcrypt 2 { 3     /** 4      * 解密 5      *  6      * @param string $encryptedText 已加密字串 7      * @param string $key  密鑰 8      * @return string 9      */10     public static function _decrypt($encryptedText,$key = null)11     {12         $key = $key === null ? Config::get('secret_key') : $key;13         $cryptText = base64_decode($encryptedText);14         $ivSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);15         $iv = mcrypt_create_iv($ivSize, MCRYPT_RAND);16         $decryptText = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $cryptText, MCRYPT_MODE_ECB, $iv);17         return trim($decryptText);18     }19  20     /**21      * 加密22      *23      * @param string $plainText 未加密字串 24      * @param string $key        密鑰25      */26     public static function _encrypt($plainText,$key = null)27     {28         $key = $key === null ? Config::get('secret_key') : $key;29         $ivSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);30         $iv = mcrypt_create_iv($ivSize, MCRYPT_RAND);31         $encryptText = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $plainText, MCRYPT_MODE_ECB, $iv);32         return trim(base64_encode($encryptText));33     }34 }

 

 

2.cookie加密解密類

 

 1 <?php 2 class Cookie extends Mcrypt 3 { 4     /** 5      * 刪除cookie 6      *  7      * @param array $args 8      * @return boolean 9      */10     public static function del($args)11     {12         $name = $args['name'];13         $domain = isset($args['domain']) ? $args['domain'] : null;14         return isset($_COOKIE[$name]) ? setcookie($name, '', time() - 86400, '/', $domain) : true;15     }16      17     /**18      * 得到指定cookie的值19      * 20      * @param string $name21      */22     public static function get($name)23     {24         return isset($_COOKIE[$name]) ? parent::_decrypt($_COOKIE[$name]) : null;25     }26      27     /**28      * 設定cookie29      *30      * @param array $args31      * @return boolean32      */33     public static function set($args)34     {35         $name = $args['name'];36         $value= parent::_encrypt($args['value']);37         $expire = isset($args['expire']) ? $args['expire'] : null;38         $path = isset($args['path']) ? $args['path'] : '/';39         $domain = isset($args['domain']) ? $args['domain'] : null;40         $secure = isset($args['secure']) ? $args['secure'] : 0;41         return setcookie($name, $value, $expire, $path, $domain, $secure);42     }43 }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.