Session can be used to store multiple types of data, so it has a lot of uses, commonly used to store user login information, shopping cart data, or some temporary use of staging data.
After the user has successfully logged in, the user's information can usually be stored in the session, the general will separate some important fields stored separately, and then all the user information stored independently.
$_session[' uid '] = $userinfo [' uid '];$_session[' userinfo '] = $userinfo;
In general, the login information can be stored in the Sessioin, but also stored in the cookie, the difference between them is that the session can easily access a variety of data types, and the cookie only supports string type, while for some security relatively high data, Cookies need to be formatted and encrypted, and session storage is more secure on the server side.
Template:
<?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;//* a simple way to save user data to a cookie */$secureKey = ' IMOOC '; Encryption Key $STR = serialize ($userinfo); Serialize user information//user information before encrypting $str = Base64_encode (Mcrypt_encrypt (mcrypt_rijndael_256, MD5 ($secureKey), $str, MCRYPT_MODE_ECB) )////The user information is encrypted//the encrypted user data is stored in a cookie Setcookie (' userinfo ', $str);//decrypted when required for use $STR = Mcrypt_decrypt (mcrypt_rijndael_256 , MD5 ($secureKey), Base64_decode ($STR), MCRYPT_MODE_ECB), $uinfo = Unserialize ($STR); echo "Decrypted user information:<br>";p rint _r ($uinfo);
PHP Learning Note: Use session to store user login information