Home  >  Article  >  Backend Development  >  PHP learning session

PHP learning session

黄舟
黄舟Original
2016-12-24 09:38:321435browse

The PHP learning content I will introduce to you today is session. So what is session and how do we use session?
The meaning of session is to complete one thing, a series of actions and messages from beginning to end. For example, when shopping online, from logging in to the website, to purchasing books, to checkout, and finally exiting, this upward shopping process can become a session. We can understand it as a collection of information records of the user by the server during the period from entering the website to closing the browser when the user browses a website.
The idea of ​​session control is to be able to track users according to a session in the website. Sessions in php are driven by unique sessionIDs. The sessionID is an encrypted random number, generated by PHP, which will be saved in the client during the life cycle of the session, or passed on the network through the URL.
sessionID is like a key, the only information visible to the client. It allows the registration of some specific variables, so it is also called a session variable. By default, session variables are saved in ordinary files on the server.
How to use session?
Basic steps for using session:
1. Start a session
2. Register session variables
3. Use session variables
4. Unregister variables and destroy session
These steps do not have to be completed in the same file , can happen in many scripts. These steps are introduced in detail below:
1. Start a session.
The first method is to call the session_start() function to start a script. This function checks whether a sessionID exists. If it does not exist, it creates a sessionID and can access it through the super global array $_SESSION. If it already exists, it loads the session variable.
The second method is to use the session.auto_start option in the php.ini file to set PHP to automatically start a session when a user visits the website. However, this way you cannot use objects as session variables.
2. Register a session variable.
You can use $_SESSION to create a session variable;
For example: $_SESSION['NAME'] = "messi";
3. Use session variables.
To use a session variable, you must use the session_start() function to activate a session first, so that the variable can be accessed through the $_SESSION global array.
When using objects as session variables, the definition of this type of object must be included before calling the session_start() function to reload the session variable, otherwise PHP cannot build the session object.
On the contrary, security issues must be considered when checking whether the session variable has been set, because the variable can be set through the get or post method. Determine whether a variable has been registered by checking the $_SESSION array.
4. Unregistering variables and destroying sessions
When a session variable is used, it should be destroyed. You can unregister the variable by destroying the appropriate elements of the array $_SESSION.
For example: unset($_SESSION['NAME'])
If you want to destroy all sessions at once, you cannot use the method of destroying the entire array $_SESSION, which will also disable the cookie function. We can solve it like this $_SESSION = array();
After eliminating all variables, the sessionID should also be destroyed. You can do this session_destroy(); This will clear the sessionID.


The above is the content of PHP learning session. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn