PHP uses sessionid to implement session sharing and login verification code. For more information, see
PHP uses session id to implement session sharing and login verification code. For more information, see
Let's talk about the purpose of this mechanism. Till now, the battlefield has known that this mechanism has two purposes:
First, we should be able to understand the problem of sharing sessions among multiple servers. When the number of users on a website is too large, a server cluster will be used, for example, a dedicated Login server. After a user logs on to the server, the login server saves the user's login information session, while other accessed servers, such as the movie server does not have this session, we need to share this session through a unique identifier of the session. The sharing of a specific session is beyond the scope of this article. Please refer to the relevant materials on your own.
The second purpose is to verify different sessions of the same user, which is hard to understand. In this case, when a user requests data not through a browser but through a socket or other method, we must first perform user login verification on the user. After the verification is successful, send a sessionid to him, and then carry this sessionid every time he requests it. We can use this sessionid to determine whether the session already exists. If so, we can determine that the user has logged on ......
For the first question, we can save the sessionid in the database for implementation. This method is safe and widely used, but it is not the scope of our discussion.
The second question is actually quite simple. Let's take a look at the code.
First, a sessionid is generated during verification;
The Code is as follows:
Session_start ();
$ SessionId = session_id (); // obtain the sessionid.
// Send the session to the client
.........
?>
The client carries the sessionid variable to request data.
The Code is as follows:
Session_id ('$ sessionid'); // note that this function includes parameters.
Session_start (); // This function must be after session_id ()
?>
At this time, the session is already used for login verification.
Note: In sessionServer. php, if thinkphp and other functions are used to automatically start session_start (), you must first call the session_destory () function to clear the session.