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