PHP Learning Note: Use session to store user login information

Source: Internet
Author: User

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

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.