Solve the problem of losing session value when passing PHP session across pages (picture and text)

WBOY
Release: 2016-07-25 09:04:20
Original
996 people have browsed it
Solve the problem of session value loss when passing PHP session across pages. If you have encountered this problem, please refer to it. The operating mechanism of session:

Solve the problem of session value loss when passing PHP session across pages. For friends who have encountered this problem, please refer to it.

Session operating mechanism: Session is a session mechanism on the server side. When the client requests the server to create a session, the server will first detect whether the request contains a unique session ID. If so, it means that the server has already created a session for the user. As long as the session ID is created, the server will Retrieve the user's session for the user to use. If there is no session ID, the server will create a new session with a unique session ID for the user. After the creation is completed, the session ID will be returned to the client by the server and saved locally on the client.

The general mechanism for saving the session ID is Cookie, but since Cookies can be artificially disabled, it is necessary to ensure that after Cookies are disabled, the session can still be conducted through the session, usually through url rewriting, in the form of http:// ...../xxx;jsessionid= ByOK3vjFD75aPnrF7C2HmdnV6QZcEbzWoWiBYEnLerjQ99zWpBng!-145788764, the other is as a query string attached to the back of the URL, in the form of http://...../xxx?jsessionid=ByOK3vjFD75aPnrF7C2HmdnV6Q ZcEbzWoWiBYEnLerjQ99zWpBng!-145788764this There is no difference between the two methods for users, but the way the server handles them during parsing is different. Using the first method is also helpful to distinguish session id information from normal program parameters. In order to maintain state throughout the entire interaction, this session id must be included at the end of every path that the client may request.

Solution to lost session: 1. session_start(); should be placed at the top of the page as much as possible; 2. If session Autostart is not configured in php.ini, you must manually open the session before each session: session_start(); 3. Session is a super global variable in PHP, the same as $_GET, $_POST, $_SERVER, so it must be capitalized when used: $_SESSION['username']=$username; 4. Cross-page transfer example: a.php page transfers $_SESSION['username'] to b.php:

a.php:

Solve the problem of losing session value when passing PHP session across pages (picture and text)

Attachment: Misunderstandings about session invalidation: When talking about the session mechanism, we often hear the misunderstanding that "as long as you close the browser, the session will disappear." In fact, you can imagine the example of a membership card. Unless the customer actively asks the store to cancel the card, the store will never delete the customer's information easily. The same is true for sessions. Unless the program notifies the server to delete a session, the server will keep it. The program usually sends an instruction to delete the session when the user logs off. However, the browser never actively notifies the server that it is about to close before closing, so the server has no chance to know that the browser has been closed. The reason for this illusion is that most session mechanisms use session cookies to save session ids. , and the session id disappears after closing the browser, and the original session cannot be found when connecting to the server again. If the cookie set by the server is saved to the hard disk, or some method is used to rewrite the HTTP request header sent by the browser and send the original session ID to the server, the original session can still be found when the browser is opened again.

It is precisely because closing the browser will not cause the session to be deleted, forcing the server to set an expiration time for the seesion. When the time since the client last used the session exceeds this expiration time, the server can consider that the client has stopped activities. , the session will be deleted to save storage space.



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