The example in this article describes how PHP uses cookies to implement user sessions. Share it with everyone for your reference. The specific analysis is as follows:
PHP contains many functions that can be used to manage and record user information, including simple cookies and full-scale user sessions. Sessions use technology built into the PHP language that makes saving state as simple as referencing a superglobal variable.
1. Cookie Introduction
We can use cookies with PHP scripts to store some smaller information about the user. A cookie is a small amount of data stored by the user's browser in response to a request from a server or script. Through a user's browser, a single host can request to save 20 cookies. Each cookie contains a name, value and expiration date, as well as host and path information. The size limit for a single cookie is 4KB.
After setting the cookie, only the host making the request can read the data, which ensures that user privacy is respected. In addition, the user can configure his browser to accept or reject all cookie requests through him. Therefore, cookies should be used sparingly and should not be relied upon as an essential element in an environment where the implementation is not designed to warn users.
If a web browser is configured to store cookies, it will retain cookie-based information until the expiration date. If the user uses the browser to browse any page that matches the path and domain of the cookie, it will resend the cookie to the server. Subsequently, a PHP script can access the cookie. The cookie is in the environment variable HTTP_COOKIE or as part of the $COOKIE superglobal variable. We can access them in 3 ways:
2. Set a cookie using PHP
We can set a cookie in a PHP script in two ways. First, use the header() function to set the SetCookie header. The Header() function expects a string that will then be included in the header portion of the server response. Since headers are automatically sent for you, header() must be called before the Jehol output is sent to the browser.
The Setcookie() function does exactly what its name suggests, it outputs a Set-Cookie header. Therefore, it should be called before any other content is sent to the browser. This function accepts the cookie name, cookie value, expiration date in UNIX timestamp format, path, domain, and an integer that is set to 1 if the cookie is only sent over a secure connection. In addition to the first parameter , all parameters of this function are optional.
Hello again, you have chosen: " . $_COOKIE [ "vegetable" ]. ".
" ;Hello you. This may be your first visit.
" ;3. Overview of session functions
When a visitor accesses a session-enabled page, either a new identifier is assigned, or the user is reassociated with an identifier already established for a previous visit. Any variables that are already associated with the session are available to your code through the $_SESSION superglobal variable.
Session state is usually stored in a temporary file, although you can implement database storage using a function called session_set_save_handler().
4. Start a conversation
Your session ID is " . session_id (). "
" ;Having access to a unique session identifier in every PHP document is just the beginning of session functionality. When a session is started, we can store as many variables as we want in the superglobal variable $_SESSION and then access them on any session-aware page.
The following program adds two variables to the superglobal variable $_SESSION:
Your products have been registered!
" ;7.销毁会话和重置变量
8.在一个带有注册用户的环境中使用会话
希望本文所述对大家的php程序设计有所帮助。