The method of encrypting and decrypting PHP operation Cookie

Source: Internet
Author: User
Tags set cookie
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.