Php implements cookie encryption. Php: how to implement cookie encryption This article mainly introduces how php implements cookie encryption. it involves php's cookie encryption, decryption, setting, and other techniques. it has some reference value, how to implement cookie encryption in php
This article mainly introduces how php implements cookie encryption. it involves the encryption, decryption, and setting of php cookies. it has some reference value. For more information, see
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:
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.
This article describes how to implement cookie encryption in php. it involves the encryption, decryption, and setting of php cookies. it has some reference value and needs...