Home  >  Article  >  Backend Development  >  What to do if php session is lost across pages

What to do if php session is lost across pages

藏色散人
藏色散人Original
2020-09-29 09:14:591948browse

Solution to php session loss across pages: first place "session_start();" at the top of the page; then configure session Autostart or manually start the session.

What to do if php session is lost across pages

Recommended: "PHP Video Tutorial"

I want to use a session to achieve this today Judging user login can also be regarded as an exploration of the previous session. After checking the information, the operating mechanism of the session is as follows:

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 created a session for the user. As long as the user's session is retrieved according to the session ID for the user to use, if there is no session ID, the server will Create a new session with a unique session ID for this user. After the creation is completed, the session ID will be returned to the client by the server and saved locally on the client.

Generally, the 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 the URL Rewriting is performed in the form of http://...../xxx;jsessionid= ByOK3vjFD75aPnrF7C2HmdnV6QZcEbzWoWiBYEnLerjQ99zWpBng!-145788764. The other is as a query string attached to the end of the URL, in the form of http://.... ./xxx?jsessionid=ByOK3vjFD75aPnrF7C2HmdnV6QZcEbzWoWiBYEnLerjQ99zWpBng!-145788764 There is no difference between these two methods for users, but the server handles them differently during parsing. Using the first method is also helpful to combine the session id information with the normal program parameters are distinguished.
In order to maintain state throughout the entire interaction process, this session id must be included at the end of each path that the client may request.

Another misunderstanding about session failure:

When talking about the session mechanism, we often hear such a misunderstanding " 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.


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 being active, and then delete the session to save storage space.

Okay, let’s talk a lot of nonsense, let’s talk about the solution to session loss:

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:

<?php
session_start();$username=$_POST[&#39;username&#39;];$_SESSION[&#39;username&#39;]=$username;
?>

b. php

880e0a19df1d92babf7ec23ac9d82cda

The above is the detailed content of What to do if php session is lost across pages. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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