Solve the problem of losing session value passing across pages in phpsession (text)

Source: Internet
Author: User
Solve the problem of losing session value passing across pages in phpsession (text)
Solve the problem of missing session value passing across pages in php sessions. if you have encountered this problem, refer. Session running mechanism:

Solve the problem of missing session value passing across pages in php sessions. if you have encountered this problem, refer.

Session running mechanism: session is a session mechanism on the server. when the client's request server creates a session, the server first checks whether the request contains a unique sessionID, if yes, the server has already created a session for this user. you only need to retrieve the session of this user based on this sesionID. If no sessionID exists, the server creates a session with a unique sessionID. After the sessionID is created, the server returns it to the client and saves it to the client.

Generally, the mechanism for saving the session ID is Cookie. However, because Cookies can be artificially disabled, you must ensure that the session can still be used after the Cookies are disabled, it is generally implemented through url rewriting, in the form of http ://..... /xxx; jsessionid = ByOK3vjFD75aPnrF7C2HmdnV6QZcEbzWoWiBYEnLerjQ99zWpBng! -145788764, the other is appended to the URL as a query string, in the form of http: //.../xxx? Jsessionid = ByOK3vjFD75aPnrF7C2HmdnV6QZcEbzWoWiBYEnLerjQ99zWpBng! -145788764 there is no difference between the two methods for users, but they are handled differently by the server during parsing, the first method also helps to distinguish the session id information from the normal program parameters. To maintain the status throughout the interaction process, the session id must be included after the path that each client may request.

Solution to session loss: 1. session_start (); should be placed at the top of the page as much as possible; 2. if php. if session Autostart is not configured in ini, you must manually enable session: session_start () before each session. 3. session is a super global variable in php, with $ _ GET, $ _ POST, $ _ SERVER is the same, so you must use uppercase letters: $ _ SESSION ['username'] = $ username; 4. cross-page transfer example:. on the php page, pass $ _ SESSION ['username'] to B. php:

A. php:

Appendix: misunderstanding about session failure: when talking about the session mechanism, we often hear the misunderstanding that "the session disappears as long as the browser is closed ". In fact, you can imagine the example of a membership card. unless the customer initiates a card sales proposal for the store, the store will never easily delete the customer's information. The same applies to Sessions. unless the program notifies the server to delete a session, the server will keep the session. Generally, the program sends a command to delete the session when the user logs off. However, the browser will never notify the server that it will be closed before it closes, so the server will not have the opportunity to know that the browser has been closed, so this illusion may occur, most session mechanisms use session cookies to save session ids. when the browser is closed, the session id disappears, and the original session cannot be found when the server is connected again. If the cookie set by the server is saved to the hard disk, or the HTTP request header sent by the browser is rewritten by some means, the original session id is sent to the server, then you can still find the original session when you open the browser 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 last time the session was used by the client exceeds this Expiration Time, the server considers that the client has stopped the activity before deleting the session to save storage space.

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.