加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php实现cookie加密解密

发布时间:2020-12-13 21:21:21 所属栏目:PHP教程 来源:网络整理
导读: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

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,1)"> MCRYPT_RAND);
16         $decryptText = mcrypt_decrypt(MCRYPT_RIJNDAEL_256,1)">$key,1)">$cryptText,MCRYPT_MODE_ECB,1)">$iv17         return trim($decryptText18     }
19  
20     21      * 加密
22      *
23      * @param string $plainText 未加密字符串 
24      * @param string $key        密钥
25      26     function _encrypt($plainText,1)">27 28         29         30         31         $encryptText = mcrypt_encrypt(MCRYPT_RIJNDAEL_256,1)">32         trim(base64_encode($encryptText));
33 34 }

?

?

2.cookie加密解密类

?

 1 <?php
class Cookie extends 3  4          * 删除cookie
     * @param array $args
     * @return boolean
function del($args$name = $args['name'];
$domain = isset($args['domain']) ? $args['domain'] : 14         $_COOKIE[$name]) ? setcookie($name,'',1)">time() - 86400,'/',1)">$domain) : true15 16      
17          * 得到指定cookie的值
19 20      * @param string $name
21      22     function get($name24         $name]) ? parent::_decrypt($name]) : 25 26      
27     28      * 设置cookie
29 30 31 32      33     function set(34 35         36         $value= parent::_encrypt($args['value']);
37         $expire = $args['expire']) ? $args['expire'] : 38         $path = $args['path']) ? $args['path'] : '/'39         40         $secure = $args['secure']) ? $args['secure'] : 041         $value,1)">$expire,1)">$path,1)">$domain,1)">$secure42 43 }

?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读