PHP Cookie operation class

Source: Internet
Author: User
PHP Cookie operation class

  1. Class Cookie
  2. {
  3. /**
  4. * Decrypt the encrypted cookie.
  5. *
  6. * @ Param string $ encryptedText
  7. * @ Return string
  8. */
  9. Private static function _ decrypt ($ encryptedText)
  10. {
  11. $ Key = Config: get ('secret _ key ');
  12. $ CryptText = base64_decode ($ encryptedText );
  13. $ IvSize = mcrypt_get_iv_size (MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB );
  14. $ Iv = mcrypt_create_iv ($ ivSize, MCRYPT_RAND );
  15. $ DecryptText = mcrypt_decrypt (MCRYPT_RIJNDAEL_256, $ key, $ cryptText, MCRYPT_MODE_ECB, $ iv );
  16. Return trim ($ decryptText );
  17. }
  18. /**
  19. * Encryption cookie
  20. *
  21. * @ Param string $ plainText
  22. * @ Return string
  23. */
  24. Private static function _ encrypt ($ plainText)
  25. {
  26. $ Key = Config: get ('secret _ key ');
  27. $ IvSize = mcrypt_get_iv_size (MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB );
  28. $ Iv = mcrypt_create_iv ($ ivSize, MCRYPT_RAND );
  29. $ EncryptText = mcrypt_encrypt (MCRYPT_RIJNDAEL_256, $ key, $ plainText, MCRYPT_MODE_ECB, $ iv );
  30. Return trim (base64_encode ($ encryptText ));
  31. }
  32. /**
  33. * Delete a cookie
  34. *
  35. * @ Param array $ args
  36. * @ Return boolean
  37. */
  38. Public static function del ($ args)
  39. {
  40. $ Name = $ args ['name'];
  41. $ Domain = isset ($ args ['domain '])? $ Args ['domain ']: null;
  42. Return isset ($ _ COOKIE [$ name])? Setcookie ($ name, '', time ()-86400, '/', $ domain): true;
  43. }
  44. /**
  45. * Obtain the value of the specified cookie.
  46. *
  47. * @ Param string $ name
  48. */
  49. Public static function get ($ name)
  50. {
  51. Return isset ($ _ COOKIE [$ name])? Self: _ decrypt ($ _ COOKIE [$ name]): null;
  52. }
  53. /**
  54. * Set cookie
  55. *
  56. * @ Param array $ args
  57. * @ Return boolean
  58. */
  59. Public static function set ($ args)
  60. {
  61. $ Name = $ args ['name'];
  62. $ Value = self: _ encrypt ($ args ['value']);
  63. $ Expire = isset ($ args ['expire '])? $ Args ['expire ']: null;
  64. $ Path = isset ($ args ['path'])? $ Args ['path']: '/';
  65. $ Domain = isset ($ args ['domain '])? $ Args ['domain ']: null;
  66. $ Secure = isset ($ args ['Secure '])? $ Args ['Secure ']: 0;
  67. Return setcookie ($ name, $ value, $ expire, $ path, $ domain, $ secure );
  68. }
  69. }


PHP, Cookie

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.