Using cookies and user sessions

Source: Internet
Author: User
Tags session id setcookie
Using cookies and user sessions

Cookies

Cookies, which store some small information about the user. It is consistent with a request from a server or script. Through a user's browser, a host can request to save 20 cookies. Each Coopkie contains a name, value and expiration date, as well as host and path information. The size limit for a single cookie is 4KB. After the cookie is set, only the requesting host can read the data, which guarantees that the user's privacy will be respected. In addition, users can configure their browser to notify him to accept or reject all requests for cookies.

The Set-cookie entry for headers sent by a PHP script that has a cookie set may be as follows:

set-cookie:vegetable=artichoke;path=/;d omain=yourdomain.com

The Set-cookie header contains:

A name/value pair (Vegetable=artichoke),

A path (path=/) and a domain (domain=yourdomain.com).

If the expiration field is set, it provides the date on which the browser "forgets" the value of the cookie. If the expiration date is not set, the cookie expires when the user session expires, that is, when the user shuts down the browser.

The path field works in conjunction with the Domain field because path is the directory where domain is found, and the cookie should be sent back to the server under this directory. If the path is "/", this is a very common value, meaning that the cookie can be read by any file in the document root directory.

If the path is "/products", this cookie can only be read by a file in the/products directory of the Web site.

Subsequently, a PHP script will be able to access Cookie,cookie in the environment variable Http_cookie or as part of the $_cookie Super global variable, which can be accessed in three ways:

echo $_server["Http_cookie"];echo getenv ("Http_cookie"); Echo $_cookie["vegetable"];

Set cookies

 
  Hello again,you Hava chosen: ". $_cookie[" vegetable "].".

";} else {echo '

Hello you. This may your first visit.

The safe way to delete a cookie is to also call the Setcookie () function, using only a time that determines that it has expired, such as: hour ()-60.

User Sessions

The session function provides the user with a unique identifier that can then be used to store and obtain information that is connected to the identifier. When a guest accesses a page that supports a session, either assigns a new identifier, or the user is re-associated with an identifier that was already established by the previous access. Any variables that have been associated with a session are made available to your code through the $_session Super global variable. Session state is typically stored in a temporary file, although you can use a function called Session_set_save_handler () to implement database storage.

Starts a session, returns the ID, and stores the variable, saved as session1.php.

 
  Your session ID is ". session_id ().".

"; $_session[" product1 "] =" Sonic screwdriver "; $_session[" product2 "] =" HAL "; echo" the products has been registered. ";? >

Access the stored session variables, use the Session_save_path () function to see where the temporary files are stored in the system, and save them as sesson2.php.

 
  "; Echo"
  • ". $_session[" Product1 "]."
  • "; Echo"
  • ". $_session[" Product2 "]."
  • "; echo"; Echo Session_save_path ();? >

    First access the server's sesson1.php, the results are as follows:

    Again access the session2.php under the server, the result is as follows:

    Find sess_curdcq4agn11gq4fdj4bq2kj33 under this path and open with Notepad to see how the registered variables are saved.

    When a value is placed in the $_session Super global variable, PHP writes the variable name and value to a file, which can be read and the variable can be recovered later. When we add this variable to the Hyper global variable $_session, you can still modify its value at any time during the execution of the script, but the modified value will not be reflected in the global setting until the variable is reassigned to the Hyper global variable $_session.

      • Size: 130.8 KB
      • Size: 129.8 KB
      • Size: 22.1 KB
      • View Picture Attachments

    The above describes the use of cookies and user sessions, including aspects of the content, I hope to be interested in PHP tutorial friends helpful.

  • Related Article

    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.