PHP code for session sharing and logon verification through sessionid

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.