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.
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['username'];$_SESSION['username']=$username; ?>
b. php
<?php session_start();echo $_SESSION['username']; ?>
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!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Chinese version
Chinese version, very easy to use

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool