Home > Article > Backend Development > Solution to the problem that PHP Session variables cannot be transferred to the next page_PHP Tutorial
I think the reasons for this problem are as follows:
1. Cookies are disabled on the client
2. There is a problem with the browser and cookies cannot be accessed temporarily
3. Session in php.ini. use_trans_sid = 0 or the --enable-trans-sid option was not turned on when compiling
Why is this happening? Let me explain below:
Session is stored on the server side (session is stored as a file by default). The user's file is obtained according to the session id provided by the client, and the value of the variable is obtained. The session id can use the client's cookie. Or the Query_String of the Http1.1 protocol (the part after the "?" of the accessed URL) is sent to the server, and then the server reads the Session directory... In other words, session id is the ID card for obtaining the session variable stored on the service. When the code session_start(); is run, a session file is generated on the server, and a session id uniquely corresponding to it is generated. The session variable is defined to be stored in the session file just generated in a certain form. Through the session id, the defined variables can be retrieved. After crossing the page, in order to use the session, you must execute session_start() again; another session file will be generated, and the corresponding session id will be generated accordingly. Using this session id, you cannot retrieve the first session file mentioned earlier. variable in because this session id is not the "key" to open it. If you add the code session_id($session id); before session_start();, a new session file will not be generated, and the session file corresponding to this id will be read directly.
The session in PHP uses the client's cookie to save the session id by default, so when there is a problem with the client's cookie, it will affect the session. It must be noted that session does not necessarily have to rely on cookies, which is also the brilliance of session compared to cookies. When the client's cookies are disabled or there is a problem, PHP will automatically attach the session id to the URL, so that the session variable can be used across pages through the session id. But this attachment also has certain conditions, namely "session.use_trans_sid = 1 in php.ini or the --enable-trans-sid option is turned on during compilation".
Friends who have used forums know that when entering the forum, you will often be prompted to check whether cookies are turned on. This is because most forums are based on cookies, and the forum uses them to save usernames and passwords. and other user information for ease of use. And many friends think that cookies are not safe (in fact, they are not) and often disable them. In fact, in PHP programs, we can use SESSION instead of Cookie, which does not depend on whether the client turns on Cookie.
So, we can put aside the cookie and use the session, that is, assuming that the user closes the cookie, use the session. There are several ways to achieve this:
1. Set the session in php.ini .use_trans_sid = 1 or enable the --enable-trans-sid option when compiling to allow PHP to automatically pass session ids across pages.
2. Manually pass the value through the URL and pass the session id through the hidden form.
3. Save the session_id in a file, database, etc., and call it manually during the cross-page process.
An example of path 1:
s1.php