Php implements cookie encryption. PHP cookie encryption _ php Tutorial

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

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.