This article mainly introduces PHP to implement the method of cookie encryption, involving PHP operation Cookie encryption, decryption and setup skills, with a certain reference value, the need for friends can refer to the next
In this paper, we explain how PHP implements cookie encryption. The implementation method is as follows:
The code is as follows:
<?phpclass 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); }/** * Encrypt cookie * * @param string $plainText * @return String */private static function _encry PT ($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 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; }/** * Gets the value of the specified cookie * * @param string $name */public static function get ($name) {retur n 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); }}
Summary : The above is the entire content of this article, I hope to be able to help you learn.