Php session cannot pass titles across pages

Source: Internet
Author: User
A friend who has applied a SESSION in PHP may encounter such a title. SESSION variables cannot be transferred across pages. This has plagued me for a few days and finally solved this title by checking materials. In my opinion, the reason for rendering this title is that friends who have applied SESSION in PHP may encounter such a title. SESSION variables cannot be passed across pages. This has plagued me for a few days and finally solved this title by checking materials. I think there are several reasons for presenting this title:
1. cookie disabled on the client
2. the browser displays the title and cannot access cookies for the moment.
3. the session. use_trans_sid in php. ini is 0 or the -- enable-trans-sid option is not enabled during compilation.

Why? Below I will explain:

The Session is stored on the server side (the session is stored as a file by default). the user's file is obtained based on the session id provided by the client, and the variable value is obtained, the session id can be used to apply the client's real Cookie or the Query_String of the Http1.1 protocol (that is, the "?" And then the server reads the Session directory ....... That is to say, session id is used to obtain the id card of the session variable stored in the service. When the code session_start (); is run, a session file is generated on the server, and a session id corresponding to it is also generated, define the session variables to be stored in the generated session file. The session id can be used to retrieve the defined variables. After a cross-page session, you must execute session_start () again to apply the session. a session file will be generated and the corresponding session id will be generated, this session id cannot be used to retrieve the variables in the first session file mentioned above, because this session id is not the "key" to open it ". If the code session_id ($ session id) is added before session_start (); no new session file is generated and the session file corresponding to this id is directly read.

By default, session IDs are retained by the client's Cookie. Therefore, session IDs are affected when the client's cookie displays the title. It is important to note that the session does not necessarily need to be attached to the cookie, which is also superior to the cookie. When the client's Cookie is disabled or the title is displayed, PHP will automatically attach the session id to the URL, so that the session variable can be applied across pages through the session id. However, this attachment also has certain conditions, that is, "session. use_trans_sid = 1 in php. ini or the -- enable-trans-sid option is enabled during compilation ".

Friends who have used the forum know that when they enter the forum, they will often prompt you to check whether the Cookie is opened. this is because most of the forums are based on cookies, the forum uses it to retain user information, such as user names and passwords, for ease of application. In addition, many friends think that cookies are not secure (this is not the case) and often disable them. In the PHP program, we can replace the Cookie with SESSION, which does not depend on whether the Cookie is enabled on the client.

Therefore, we can leave the cookie application session, that is, if the user closes the cookie and applies the session, there are the following paths:

1. set session. use_trans_sid = 1 in php. ini or enable the -- enable-trans-sid option during compilation, so that PHP can actively pass the session id across pages.
2. manually pass session IDs through URL values and hidden forms.
3. retain session_id in the form of files and databases and manually call it during the cross-page process.

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.