PHP Session Cookie Encryption Instance

Source: Internet
Author: User
Tags php session

The difference between session cookies is that the session can easily access a variety of data types, while the cookie only supports string types, and for some high-security data, cookies need to be formatted and encrypted for storage. The session is stored on the server side with high security.

<?phpsession_start ();//Assume that the user logged in successfully obtained the following user data $userinfo = array (' uid ' = ' = '  10000,    ' name ' = ' spark ',    ' Email ' = ' [email protected] ',    ' sex '  = ' man ', ' age ' = ', ' The  header ("Content-type: text/html; Charset=utf-8 ");/* Save user information to SESSION */$_session[' uid '] = $userinfo [' uid '];$_session[' name '] = $userinfo [' Name '];$_ session[' userinfo ' = $userinfo; echo "Welcome". $_session[' name ']. ' <br> ';//* a simple way to save user data to a cookie */$secureKey = ' IMOOC '; Encryption Key $STR = serialize ($userinfo); Serializes the user information before the echo "User information is encrypted:". $str; $str = Base64_encode (Mcrypt_encrypt (mcrypt_rijndael_256, $secureKey, $str, Mcrypt_ MODE_ECB)); echo "User information is encrypted:". $str;//store encrypted user data in a cookie Setcookie (' userinfo ', $str);//decrypt when required to use $STR = Mcrypt_decrypt (mcrypt_rijndael_256, $secureKey, Base64_decode ($STR), MCRYPT_MODE_ECB), $uinfo = Unserialize ($STR); echo "Decrypted user information: <br> "; Var_dump ($uinfo);

  

PHP Session Cookie Encryption Instance

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.