: This article mainly introduces PHP Session management _ Session. For more information about PHP tutorials, see. 1. understand Session
The Session starts from the user's access to the page and ends with the website being disconnected, forming the Session lifecycle. Every time a user connects, PHP automatically generates a unique SessionID to identify the current user and distinguish it from other users.
During a session, PHP generates the PHPSESSIONID by default. ini file), which is sent to the browser along with each page, and then returned to the Web server with the next page request.
SessionID can be saved to the database as session information and used as the Primary Key to identify different users or as a unique string in the session file name on the server side.
Session IDs are stored on the client and server respectively. On the client side, temporary cookies are saved in the specified directory of the browser (as Session Cookie); on the server side, Session cookies are saved in the specified Session directory as text files.
II. start the Session
Passsession_start()Function creation session
Bool session_start (void );
Note: Usesession_start()In the past, the browser could not have any output; otherwise, an error would occur.
Passsession_register()Function creation session
session_register()The function is used to implicitly start the session by logging on to a variable for the session, but requires the option of the php. ini file to set the register_globals command to 'on' and restart the Apache server.
3. apply Session
The Session function in PHP is powerful: it can save the specific data and related information of the current user. You can save any data type of arrays, objects, and strings. To add various types of data to a Session, a global array must be applied.$_SESSION[].
4. delete a Session
Delete a single session
Delete session variables. log out directly like the operation on the array.$_SESSIONAn element of the array.
Unset ($ _ SESSION ['whe']);
Delete multiple sessions
To cancel all session variables at a time, you can assign an empty array$_SESSION
$ _ SESSION = array ();
End current session
If the entire session has ended, you should first cancel all session variables and then usesession_destroy()The function clears the current Session, clears all resources in the Session, and permanently destroys the Session.
Session_destroy ();
V. Session application
- Use Session to control page access permissions.
VI. Comparison between cookies and Sessions
The biggest difference is:
- Session stores information on the server and transmits the client information through a Session ID. after receiving the Session ID, the server provides related Session information resources based on this ID;
- Cookie stores all information on the client as text files and is managed and maintained by the browser.
- The Session is stored on the server. the client user cannot modify the Session file. The Cookie is stored on the client, and the Session is much safer than the Cookie.
The above introduces PHP Session management _ Session, including some content, and hopes to help friends who are interested in PHP tutorials.