Session Tracking Technology--cookie

Source: Internet
Author: User
Tags send cookies setcookie time and date

One, session control
    • In order for the Web site to track the interaction between the client and the server, to save and remember each user's identity and information, we need a powerful solution that generates session control.
    • HTTP is a stateless protocol that cannot be maintained to maintain a connection between two transactions.
    • When a user requests a page and then requests another page, HTTP cannot tell us that the two requests came from the same person.
    • The idea of session control is to be able to track a variable in the Web site, we can track the variables, we can do the user support, and according to the authorization and user identity display different content, different pages.
second, what is a cookie?
    • A cookie is a way for a server or script to maintain client information under the HTTP protocol.
    • A cookie is a cookies (a small text file) that a Web server holds on a user's browser and can contain information about a user, often used to save usernames, passwords, personalization settings, personal preferences, and so on.
    • When a user accesses a server, the server can set and access cookie information.
    • Cookies are stored on the client, usually in the cookie Temp folder of IE or Firefox browser, and can be deleted manually.
    • Cookies are transmitted using the HTTP header information in the Code of the Web page, and each page request from the browser can be accompanied by a cookie.
    • Note: If there is too much cookie on the browser, the browser will automatically delete it if it exceeds the range allowed by the system.
third, set the cookie
    • BOOL Setcookie (string name, [string Value, [int expire, [string path, [string Domain, [int secure]]]);
      • Name sets the name of the cookie. (required)
      • Value sets the values of the cookie.
      • Expire set the expiration time and date of the cookie, using a standard UNIX timestamp, which can be obtained in seconds using the time () function.
      • Optional parameters path, domain, secure
        • Path: The server-side valid path, set to "/" indicates that all directories in this domain can be accessed for reading.
        • Domain: Set a valid cookie domain name
        • Secure: Sets whether to send cookies to the client, 0 or 1 only when HTTPS is secured.
    • Example:
      Setcookie ("User", "Tangcaiye",Time () +60*60);

Iv. receipt and use of cookies

    • PHP has good support for cookies and, like form forms, PHP automatically receives HTTP headers from the Web server and parses it when it is received. Receive the same time as the form receives
    • Example:
      Echo $_cookie ["username"];
v. Deletion of cookiesTo delete a cookie that already exists,There are two ways .:
    • Call the Setcookie with the name parameter only, then the cookie named will be deleted from the client;
      • Example:
        Setcookie ("MyCookie");
    • Set the cookie to expire at time () or 1
      • Example:
        Setcookie ("username", "admin",Time ()-1);
      • Attention:
        • Time () minus how much does not matter, as long as the expiration of the line, then this cookie on this page after browsing has been deleted (in fact, it is invalid).
        • When a cookie is deleted, its value is still valid on the current page.
        • If you want to set the cookie to expire after the browser is closed. You can set Expiretime to 0 directly or not. Example: Setcookie ("name", "value", 0).

Session Tracking Technology--cookie

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.