Home > Backend Development > PHP Tutorial > PHP code to implement session sharing and login verification through session id_PHP tutorial

PHP code to implement session sharing and login verification through session id_PHP tutorial

WBOY
Release: 2016-07-21 15:19:01
Original
780 people have browsed it

Let’s talk about the purpose of this mechanism first. Until now, Battlefield knows that this mechanism has two uses:

First of all, the problem of multi-server sharing session, everyone should be able to understand this. When a website If the number of users is too large, a server cluster will be used, for example, a dedicated server for login. After the user logs in through the login server, the login server saves the user's login information session, and other accessed servers, such as the movie server, do not have this session, then we have to share this session through a unique identifier of the session - the specific session The sharing is beyond the scope of this article, please check the information yourself.

The second purpose is to verify different sessions of the same user, which is more difficult to understand. Let's put it this way, when a user does not request a connection through a browser, but requests data through a socket or other methods, we must first perform user login verification on him. After the verification is successful, we will issue a sessionid to him, and then He carries this sessionid every time he makes a request. We use this sessionid to determine whether the session already exists. If it exists, we assume that the user has logged in...

For the first question, we can save the sessionid in It can be implemented in the database. This method is relatively safe and widely used, but it is not the scope of our discussion

The second question is actually very simple. Take a look at the code

When verifying it first Generate a sessionid;

Copy code The code is as follows:

Session_start();
$sessionId = session_id();//Get sessionid
//Delivery session to client
.........
?>

The client carries the sessionid variable to request data
Copy code The code is as follows:

Session_id( '$sessionid');//Note that the session_id() function has parameters at this time
Session_start();//This function must be after session_id()
?>

At this time, the session is already the session during login verification.
Note: If you use thinkphp and other other frameworks that automatically start the session_start(); function in sessionServer.php, you must first call the session_destory() function to clear the session.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325408.htmlTechArticleLet’s first talk about the purpose of this mechanism. Until now, Battlefield knows that this mechanism has two uses: First of all, the problem of multi-server sharing sessions, everyone should be able to understand this...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template