Use cookies and user sessions

Source: Internet
Author: User
Tags set cookie
: This article describes how to use cookies and user sessions. For more information about PHP tutorials, see. Use cookies and user sessions

Cookie

Cookie, which stores small user information. It is consistent with a request from a server or script. A user's browser allows a host to request 20 cookies to be saved. each coopkie contains a name, value, expiration date, and host and path information. The size limit of a single cookie is 4 kB. After a cookie is set, only the requesting host can read data, which ensures that user privacy is respected. In addition, you can configure your browser to notify him to accept or reject all cookie requests.

Set-cookie entries in the header sent by a PHP script with a Cookie Set may be as follows:

Set-Cookie: vegetable = artichoke; path =/; domain = yourdomain.com

The Set-Cookie header contains:

A name/value pair (vegetable = artichoke ),

One path (path =/) and one domain (domain = yourdomain.com ).

If the expiration field is set, it provides the date on which the browser "forgets" the cookie value. If no expiration date is set, when the user session expires, that is, when the user closes the browser, the cookie will expire.

The path field works with the domain Field. Because path is a directory of the domain, the cookie should be sent back to the Directory of the server. If the path is "/", this is a common value, meaning that the cookie can be read by any file in the root directory of the document.

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

Then, a PHP script will be able to access cookies. cookies can be accessed in the environment variable HTTP_COOKIE or as part of the $ _ cookie hyper-global variable in three ways:

echo $_SERVER["HTTP_COOKIE"];echo getenv("HTTP_COOKIE");echo $_COOKIE["vegetable"];

Set Cookie

 Hello again, you hava chosen: ". $ _ COOKIE [" vegetable "].".

";} Else {echo"

Hello you. This may be your first visit.

";}?>

The secure method for deleting a cookie also calls the setcookie () function. you only need to use a time to determine the Expiration time, for example, time ()-60.

User session

The session function provides a unique identifier, which can be used to store and obtain information connected to the identifier. When a visitor accesses a page that supports sessions, either a new identifier is assigned or the user is re-associated with an identifier that has been created for the previous access. Any variables associated with the SESSION are made available to your code through the $ _ SESSION Super global variable. Session status is usually stored in a temporary file, although you can use a function named session_set_save_handler () to implement database storage.

Start a session, return the ID, store the variable, and save it as session1.php.

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

";$_SESSION["product1"] = "Sonic Screwdriver";$_SESSION["product2"] = "HAL 2000";echo "The products have been registered.";?>

Access the stored session variable. use the session_save_path () function to view where the temporary file is saved in the system and save it as sesson2.php.

 ";echo "
  • ".$_SESSION["product1"]."
  • ";echo "
  • ".$_SESSION["product2"]."
  • ";echo "";echo session_save_path();?>

    First, access sesson1.php on the server. The result is as follows:

    Then access session2.php on the server. The result is as follows:

    In this path, find sess_curdcq4agn11gq4fdj4bq2kj33 and open it in notepad to see how the registered variables are saved.

    When a value is placed in a $ _ SESSION Superglobal variable, PHP writes the variable name and value to a file. This information can be read and the variable can be restored later. After we add this variable to the Superglobal variable $ _ SESSION, you can modify the value at any time during script execution. However, the modified value is not reflected in the global settings until the variable is reassigned to the Super global variable $ _ SESSION.

    • The size is 130.8 KB.
    • The size is 129.8 KB.
    • The size is 22.1 KB.
    • View image attachments

    The above describes how to use cookies and user sessions, including related 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.