Home  >  Article  >  Backend Development  >  What is the reason why php session is lost?

What is the reason why php session is lost?

藏色散人
藏色散人Original
2021-03-01 09:23:413404browse

The reasons for php session loss are: 1. Cookies are disabled on the client; 2. The browser cannot access cookies; 3. "session.use_trans_sid=0" in php.ini is not turned on during compilation" – enable-trans-sid" option.

What is the reason why php session is lost?

The operating environment of this article: Windows 7 system, PHP 8 version, Dell G3 computer.

The author accidentally encountered a problem. The customer said that the session of the website was always lost. Then I searched for a long time and never found the reason. Finally, I found out that the time on the server did not match the network time, which caused the problem. The user cannot log in or logs out immediately. In the process, I discovered that there may be other reasons, which I will show here and share with you.

Generally speaking, there are the following reasons for SESSION to be lost:

1. The client has disabled cookies

2. The browser cannot access it cookie

3, session.use_trans_sid=0 in php.ini or the –enable-trans-sid option is not turned on when compiling. Session is stored on the server side (by default, it is stored in a file), according to the session provided by the client. id to get the user's file and get the value of the variable. The session id can be sent to the server using the client's cookie or the Query_String of the Http1.1 protocol (that is, the part after the "?" of the accessed URL), and then the server reads the session's Table of contents…….

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(); another session file will be generated, corresponding to which the corresponding session id will be generated. This session id cannot be used to retrieve the previously mentioned variable in the first session file, 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. By default, the session in PHP uses the client's cookie to save the session id, 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, that is, "session.use_trans_sid = 1 in php.ini or the –enable-trans-sid option is turned on during compilation". Understand the above principles, now let’s put aside cookies and use sessions. There are three main ways:

1. Set session.use_trans_sid = 1 in php.ini or turn on –enable-trans- during compilation. The sid option allows PHP to automatically pass the session id across pages.

2. Manually pass the value through the URL and pass the session id through the hidden form.

3. Save session_id in a file, database, etc., and call it manually during the cross-page process.

[Recommended learning: "PHP Video Tutorial"]

The above is the detailed content of What is the reason why php session is lost?. 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