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