The Session variable cannot be transmitted to the next page. Solution: session. use_trans_sid = 1, usetranssid

Source: Internet
Author: User
Tags define session

The Session variable cannot be transmitted to the next page. Solution: session. use_trans_sid = 1, usetranssid

Appendix: Abstract
========================================================== ================================
Friends who have used SESSION in PHP may encounter such a problem. SESSION variables cannot be transferred across pages. This has plagued me for a few days and finally solved this problem by checking the information. I think the reasons for this problem are as follows:
1. cookie disabled on the client
2. a browser error occurs and the cookie cannot be accessed 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? I will explain the following:

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 use the Cookie of the client 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 session variables to be stored in the generated session file in a certain form. The session id can be used to retrieve the Defined variables. After a cross-page session, you must execute session_start (); a session file is generated, and the corresponding session id is 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, the session in PHP uses the Cookie of the client to save the session id. Therefore, when the cookie of the client fails, the session will be affected. It must be noted that the session does not necessarily depend on the cookie, which is also a bit better than the cookie. When the Cookie on the client is disabled or a problem occurs, PHP automatically attaches the session id to the URL, so that the session variable can be used 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 ".

All the 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 save user information such as user names and passwords for ease of use. In addition, many friends think that cookies are not safe (in fact, they are not) and often disable them. In PHP, we can use SESSION instead of Cookie. It does not depend on whether the client enables Cookie.

Therefore, we can leave the cookie to use the session, that is, assume that the session is used when the user closes the cookie. There are several ways to implement the session:

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

Example of Method 1:

S1.php

<? Php
Session_start ();
$ _ SESSION ['var1'] = "People's Republic of China ";
$ Url = "<a href =". "" s2.php "> next page </a> ";
Echo $ url;
?>

S2.php

<? Php
Session_start ();
Echo "the value of the passed session variable var1 is:". $ _ SESSION ['var1'];
?>

Run the above Code. When the client cookie is normal, you can obtain the result "People's Republic of China ".
Now you can manually close the client cookie and run it again. The result may not be returned. If no result is returned, "set session. use_trans_sid = 1 in php. ini or enable the -- enable-trans-sid option when compiling". The "People's Republic of China" is returned"

 

Example of Method 2:

S1.php

<? Php
Session_start ();
$ _ SESSION ['var1'] = "People's Republic of China ";
$ Sn = session_id ();
$ Url = "<a href =". "" s2.php? S = ". $ sn." "> next page </a> ";
Echo $ url;
?>

S2.php

<? Php
Session_id ($ _ GET ['s ']);
Session_start ();
Echo "the value of the passed session variable var1 is:". $ _ SESSION ['var1'];
?>

The basic principle of hiding a form is the same as above.

Example 3:

Login.html

<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Title> Login </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
</Head>
<Body>
Please log on:
<Form name = "login" method = "post" action = "mylogin1.php">
Username: <input type = "text" name = "name"> <br>
Command: <input type = "password" name = "pass"> <br>
<Input type = "submit" value = "login">
</Form>
</Body>
</Html>

Mylogin1.php

<? Php

$ Name = $ _ POST ['name'];
$ Pass = $ _ POST ['pass'];
If (! $ Name |! $ Pass ){
Echo "the user name or password is blank. Please <a href =" login.html "> log on again </a> ";
Die ();
}
If (! ($ Name = "youngong" & $ pass = "123 "){
Echo "the user name or password is incorrect. Please <a href =" login.html "> log on again </a> ";
Die ();
}
// Register a user
Ob_start ();
Session_start ();
$ _ SESSION ['user'] = $ name;
$ Psid = session_id ();
$ Fp = fopen ("e:/tmp/phpsid.txt", "w + ";
Fwrite ($ fp, $ psid );
Fclose ($ fp );
// Complete the authentication.
Echo "logged on <br> ";
Echo "<a href =" mylogin2.php "> next page </a> ";

?>

Mylogin2.php

<? Php
$ Fp = fopen ("e:/tmp/phpsid.txt", "r ";
$ Sid = fread ($ fp, 1024 );
Fclose ($ fp );
Session_id ($ sid );
Session_start ();
If (isset ($ _ SESSION ['user']) & $ _ SESSION ['user'] = "laogong "{

Echo "logged on! ";
}
Else {
// Log on successfully for related operations
Echo "not logged on, not authorized to access ";
Echo "Please <a href =" login.html "> log on </a> and browse ";
Die ();
}

?>

Similarly, disable the cookie test. Username: youngong password: 123 this is the session id saved through the file, and the file is: e: mpphpsid.txt. Please decide the file name or path based on your system.

As for the database method, I will not give an example. It is similar to the file method.

To sum up, the above method has one thing in common: Get the session id in the previous page, and then try to pass it to the next page, in the session_start () of the next page (); before the code, add the code session_id (the passed session id );
========================================================== ======================================

Also, my php. the INI file is stored in 1.c:/php4/php. ini 2.c:/winnt/php. ini, and my program seems to only know c:/php4/php. ini, for php. ini changes do not work .!!!
It is better to change the two files at the same time when it is unclear which php. ini works. It is troublesome, but it can solve the problem.


I am a luix system and set sessionuse_trans_sid = 1 in phpini. The session still cannot be passed across pages. Which of the following prawns can help me?

Www.qiuqie.com
Search for php. ini

If you need to pass the variable value in a B/S structure, but you cannot use Session, Cookie, and Application, you can enter

This. Server. Transfer

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.