Use session to store user's login information

Source: Internet
Author: User

To encrypt the existence of cookie-side data, the code is as follows:

<?PHPSession_Start();//Assuming the user is logged on successfully, the following user data is obtained$userinfo=Array(    ' UID ' = 10000, ' name ' = ' spark ', ' email ' + ' [email protected] ', ' sex ' = ' man ', ' age ' = ' 18 ');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);//Serialization of user informationEcho"Before user information is encrypted:".$str.‘ <br> ';$str=Base64_encode(Mcrypt_encrypt (mcrypt_rijndael_256,$secureKey,$str,MCRYPT_MODE_ECB));Echo"After user information is encrypted:".$str.‘ <br> ';//Store encrypted user data in a cookieSetcookie(' UserInfo ',$str);//decrypt when needed for 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);Exit;

Use session to store user's login information

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.