Then explore PHPsession. Reprinted please indicate from souldak, Weibo: @ evagle the previous session and cookie are still vague. today, when using the yii Framework, session_regenerate_id (): Sessionobjectdestr reprinted please indicate from souldak, Weibo: @ evagle
In the past, the session and cookie were still vague. today, when I used the yii Framework, I encountered the session_regenerate_id (): Session object destruction failed error, so I looked at the working mechanism of the session.
Capture the definition of a session from W3Cschool:
When you run an application, you open it, make some changes, and then close it. This is like a session. The computer knows who you are. It knows when to start the application and when to terminate it. However, there is a problem on the Internet: the server does not know who you are and what you are doing, because the HTTP address cannot be maintained.
By storing user information on the server for subsequent use, PHP session solves this problem (such as user name and product purchase ). However, the session information is temporary and will be deleted after the user leaves the website. If you need to store information permanently, you can store the data in the database.
The Session mechanism is to create a unique id (UID) for each visitor and store the variables based on the UID. The UID is stored in the cookie or transmitted through the URL.
Let's take a look at the session lifecycle, which is very important for understanding the session.
1. When will a session be created?
Simply put, Sessinon is created when the user accesses the server for the first time. The session is created in php, jsp, asp, and other web languages, and will not be created when accessing static html pages. Let's take php as an example:
A) when user X accesses a php page, if the page starts with the session_start () function, the server creates a session and generates a session_id. This ID is unique and varies with users.
B) this session id will be stored in the $ _ SESSION variable of PHP. we can also set the cookie on the user's browser.
C) when the user jumps to another page of the website, he or she only needs to tell the browser the session id stored in the cookie, and the server will retrieve the storage information corresponding to the session id, you will know who the user is, and the user's information will be displayed on another page.
D) after the session is created, you can store information related to other users, such as the items in the user's shopping cart.
E) after a Session is generated, as long as the user continues to access the Session, the server will update the last access time of the Session and maintain the Session. Each time a user accesses the server, whether or not the Session is read or written, the server considers the user's Session to be "active" once.
Therefore, when a user accesses a website, the process from opening to ending (unless the session expires too quickly) is normally in the same session, no matter how he jumps to the website, can access the content of this session.
The session_id () function in PHP shows the current session_id information. Because a website can be accessed by multiple users at the same time, PHP generates a unique session_id for each user. This will not interfere with each other, because every user is processed by an independent process. the process in which user A is located stores the session_id of user A until the session becomes invalid.
In short, the user usually sets a session when logging on to the website, and closes or clears the session when the user exits (close or clear as needed ).
2. When will the session expire?
A) as more and more users access the server, more and more sessions will be held. To prevent memory overflow, the server deletes sessions that are not active for a long time from the memory. This is the Session timeout time. If the server has not been accessed after the timeout, the Session will automatically fail.
B) call the invalidate method of the Session.
Reference: http://www.w3school.com.cn/php/php_sessions.asp
Then several common PHP session-related functions are used:
Session_start-Start new or resume existing session Start
Session_id-Get and/or set the current session id to Get the session id
Session_status-Returns the current session status to get the session status, which has three statuses
PHP_SESSION_DISABLEDIf sessions are disabled.
PHP_SESSION_NONEIf sessions are enabled, but none exists.
PHP_SESSION_ACTIVEIf sessions are enabled, and one exists. session_regenerate_id-Update the current session id with a newly generated one for the current session to be reassigned
Session_destroy-Destroys all data registered to a session completely deletes the entire session
Session_unset-Free all session variables delete all variables stored in the current session
For more information, see: http://www.php.net/manual/en/ref.session.php
Previously, session and cookie are still vague. today, when using the yii Framework, session_regenerate_id (): Session object destr...