Php implements cookie encryption and phpcookie encryption. Php implements cookie encryption. phpcookie encryption This article describes how php implements cookie encryption. Share it with you for your reference. The specific implementation method is as follows: Copy the code php to implement cookie encryption, phpcookie encryption
This example describes how to implement cookie encryption in php. Share it with you for your reference. The specific implementation method is as follows:
The code is as follows: <? Php
Class Cookie
{
/**
* Decrypt the encrypted cookie.
*
* @ Param string $ encryptedText
* @ Return string
*/
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 );
}
/**
* Encryption cookie
*
* @ Param string $ plainText
* @ Return string
*/
Private static function _ encrypt ($ plainText)
{
$ 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 );
Return trim (base64_encode ($ encryptText ));
}
/**
* Delete a cookie
*
* @ Param array $ args
* @ Return boolean
*/
Public static function del ($ args)
{
$ Name = $ args ['name'];
$ Domain = isset ($ args ['domain '])? $ Args ['domain ']: null;
Return isset ($ _ COOKIE [$ name])? Setcookie ($ name, '', time ()-86400, '/', $ domain): true;
}
/**
* Obtain the value of the specified cookie.
*
* @ Param string $ name
*/
Public static function get ($ name)
{
Return isset ($ _ COOKIE [$ name])? Self: _ decrypt ($ _ COOKIE [$ name]): null;
}
/**
* Set cookie
*
* @ Param array $ args
* @ Return boolean
*/
Public static function set ($ args)
{
$ 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;
Return setcookie ($ name, $ value, $ expire, $ path, $ domain, $ secure );
}
}
I hope this article will help you with php programming.
Examples in this article describes how php implements cookie encryption. Share it with you for your reference. The specific implementation method is as follows: code...