PHP操作Cookie的类

2025-10-26 20:51:03

1、解密已经加密了的cookie

private static function _decrypt($encryptedText)    {        

$key = Config::get('secret_key');         

$cryptText = base64_decode($encryptedText);         

$ivSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);        

$iv = mcrypt_create_iv($ivSize, MCRYPT_RAND);        

$decryptText = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $cryptText, MCRYPT_MODE_ECB, $iv);        

return trim($decryptText);   

 }

PHP操作Cookie的类

2、加密cookie

$key = Config::get('secret_key'); 

$ivSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);

$iv = mcrypt_create_iv($ivSize, MCRYPT_RAND);

$encryptText = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $plainText, MCRYPT_MODE_ECB, $iv);

$encryptText  = trim(base64_encode($encryptText));

PHP操作Cookie的类

3、删除cookie

 $name = $args['name'];

 $domain = isset($args['domain']) ? $args['domain'] : null;

  isset($_COOKIE[$name]) ? setcookie($name, '', time() - 86400, '/', $domain) : true;

PHP操作Cookie的类

4、得到指定cookie的值

isset($_COOKIE[$name]) ? self::_decrypt($_COOKIE[$name]) : null;

PHP操作Cookie的类

5、设置cookie

        $name = $args['name'];

        $value= self::_encrypt($args['value']);

        $expire = isset($args['expire']) ? $args['expire'] : null;

        $path = isset($args['path']) ? $args['path'] : '/';

        $domain = isset($args['domain']) ? $args['domain'] : null;

        $secure = isset($args['secure']) ? $args['secure'] : 0;

        setcookie($name, $value, $expire, $path, $domain, $secure);

PHP操作Cookie的类

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
相关推荐
  • 阅读量:108
  • 阅读量:107
  • 阅读量:39
  • 阅读量:114
  • 阅读量:148
  • 猜你喜欢