Home >Backend Development >PHP Tutorial >Another look at PHP session_PHP tutorial
Please indicate when reprinting from soulak, Weibo: @evagle
In the past, I was still vague about sessions and cookies. Today when I used the Yii framework, I encountered the error session_regenerate_id(): Session object destruction failed, so I took a look at the working mechanism of the session again.
Intercept the definition of session from W3Cschool:
When you run an app, you open it, make changes, and then close it. It's a lot like a session. The computer knows who you are. It knows when you start the application and when it terminates it. But on the Internet, there's a problem: the server doesn't know who you are and what you do, and that's because HTTP addresses don't maintain state.
PHP session solves this problem by storing user information on the server for subsequent use (such as user name, purchased items, etc.). However, session information is temporary and will be deleted after the user leaves the site. If you need to store information permanently, you can store the data in a database.
Session works by creating a unique id (UID) for each visitor and storing variables based on this UID. The UID is stored in a cookie or passed through the URL.
Let’s first take a look at the life cycle of the session, which is very important for understanding the session.
1. When is the session created?
Simply put, Session is created when the user accesses the server for the first time. Session is created by web languages such as php, jsp, asp, etc. It will not be created when accessing static html pages. Let’s take php as an example:
a) When user This ID is unique and different for each user.
b) This session id will be stored in the $_SESSION variable of PHP, and we can also set a cookie on the user's browser.
c) Then when the user jumps to other pages of the website, he only needs to tell the browser the session id stored in this cookie, and then the server will take out the stored information corresponding to the session id, and then know who the user is. A page can also display this information about the user.
d) After the session is created, other user-related information can be stored, such as storing item information in the user's shopping cart.
e) After the Session is generated, as long as the user continues to access, the server will update the last access time of the Session and maintain the Session. Every time a user accesses the server, regardless of whether the session is read or written, the server considers the user's session to be "active".
So, when a user visits a website, normally he is in the same session from opening to end (unless the session expires too quickly). No matter how he jumps in the website, he can access the session content.
The session_id() function in PHP can see the current session_id information. Because a website can be visited by many users at the same time, PHP will generate a unique session_id for each user. This will not interfere with each other, because each user is handled by an independent process. The process where user A is located stores A's session_id until the session expires.
In short, the session is generally set up when the user logs in to the website, and then closed or cleaned up when the user logs out (closing or cleaning depends on business needs).
2. When does the session expire?
a) As more and more users will access the server, there will be more and more Sessions. To prevent memory overflow, the server will delete Sessions that have not been active for a long time from memory. This time is the Session timeout time. If the server is not accessed after the timeout period, the Session will automatically expire.
b) Call the invalidate method of Session.
Reference: http://www.w3school.com.cn/php/php_sessions.asp
Then there are several commonly used PHP session-related functions:
session_start — Start new or resume existing session sessionStart
session_id — Get and/or set the current session id Get session id
session_status — Returns the current session status to get session status, there are three statuses
PHP_SESSION_DISABLED
if sessions are disabled.PHP_SESSION_NONE
if sessions are enabled, but none exists. if sessions are enabled, and one exists.
session_regenerate_id — Update the current session id with a newly generated one to reassign the current sessionPHP_SESSION_ACTIVE
For more information, please refer to: http://www.php.net/manual/en/ref.session.php