PHP --- cookie

Source: Internet
Author: User
: This article mainly introduces PHP-cookie. if you are interested in the PHP Tutorial, you can refer to it.

The server tracks and stores user data, which exists in the HTTP header and is stored in the global variable $ _ COOKIE.
Identify users

Setcookie function

Name $ _ COOKIE ['name'] accessible
Value
Expire expiration time
Path valid path,/the entire website takes effect
Domain valid domain by default, the entire domain name is valid

Restrictions on using cookies

  • The cookie is not too secure and is prone to theft, resulting in cookie spoofing.
  • The maximum value of a single cookie is 4 kB.
  • Network transmission is required for each request, occupying the bandwidth
  • Cookie only supports string type

Session

Session_start () enabled

Global variable _ $ SESSION is used to save session data and save it on the server as a file (cookie is saved on the client)

When the page is opened, this session file is exclusive, causing other concurrent accesses of the current user to be unable to be executed and waiting. it can be stored as a cache or database.

Unset () deletes a session value.
Session_destroy () deletes all data, but session_id still exists. The deletion mechanism is to delete session data the next time you access it.


  10000, 'name' => 'Spark ', 'Email' => 'spark @ imooc.com ', 'sex' => 'Man ', 'age' = & gt; '18'); header ("content-type: text/html; charset = utf-8 "); /* save user information to the session */$ _ SESSION ['uid'] = $ userinfo ['uid']; $ _ SESSION ['name'] = $ userinfo ['name']; $ _ SESSION ['userinfo'] = $ userinfo; // * a simple method for saving user data to cookies */$ secureKey = 'imooc '; // encryption key $ str = serialize ($ userinfo ); // serialize user information // $ str = base64_encode (mcrypt_encrypt (MCRYPT_RIJNDAEL_256, md5 ($ secureKey), $ str, MCRYPT_MODE_ECB) before user information encryption )); // after the user information is encrypted // The encrypted user data is stored in the cookie setcookie ('userinfo', $ str ); // when necessary, decrypt $ str = mcrypt_decrypt (encrypt, md5 ($ secureKey), base64_decode ($ str), MCRYPT_MODE_ECB); $ uinfo = unserialize ($ str ); echo "decrypted user information:
"; Print_r ($ uinfo );

'). AddClass ('pre-numbering '). hide (); $ (this ). addClass ('Has-numbering '). parent (). append ($ numbering); for (I = 1; I <= lines; I ++) {$ numbering. append ($ ('
  • '). Text (I) ;}; $ numbering. fadeIn (1700) ;}) ;}; script

    The above introduces PHP --- cookie, including some content, and hope to help friends who are interested in PHP tutorials.

    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.