In PHP, the SESSION cannot be transferred across pages. a friend who has used the SESSION in PHP may encounter this problem. The SESSION variable 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 :? Solution to cross-page transfer failure of SESSION in PHP
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. does the client disable cookies?
2. Why is my browser unable to access cookies?
3. session. use_trans_sid in php. ini? =? 0 or is the -- enable-trans-sid option 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? Id to get the user's file, get the variable value, session? Id can use the Cookie of the client or Query_String of the Http1.1 protocol (that is, the "?" of the Accessed URL And then the server reads the Session directory ....... That is, session? Id is the id of the session variable stored in the service. When the code session_start (); is running, a session file is generated on the server, and a session corresponding to it is also generated? The session variable is stored in the generated session file in a certain form. Through session? Id to retrieve the defined variables. After a cross-page session, you must execute session_start (); a session file will be generated, and corresponding sessions will be generated? Id. use this session? The id cannot retrieve the variables in the first session file mentioned above, because this session? Id is not the "key" to open it ". If you add the code session_id ($ session? Id); no new session file will be generated, and the session file corresponding to this id will be directly read .?
By default, the session in PHP uses the client Cookie to save the session? So when the client cookie is faulty, 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 client's Cookie is disabled or a problem occurs, PHP will automatically set the session? Id is attached to the URL, and then the session? Id to use the session variable across pages. But this attachment also has certain conditions, that is, "session. use_trans_sid in php. ini? =? 1. 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, if the user closes the cookie and uses the session, there are several ways to implement it :?
1. set session. use_trans_sid in php. ini? =? 1. the -- enable-trans-sid option is enabled during compilation so that PHP can automatically pass sessions across pages? Id .?
2. manually pass the session through URL-based values or hide forms? Id .?
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?
Session_start ();?
$ _ SESSION ['var1'] = "People's Republic of China ";?
$ Url = "next page ";?
Echo? $ Url ;?
?>?
S2.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 in php. ini? =? 1. enable the -- enable-trans-sid option when compiling, and get the result "People's Republic of China "?
Example of Method 2 :?
S1.php?
Session_start ();?
$ _ SESSION ['var1'] = "People's Republic of China ";?
$ Sn? =? Session_id ();?
$ Url = "next page ";?
Echo? $ Url ;?
?>?
S2.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?
?
?
?
Login?
?
?
?
Log on :?
?
?
?
Mylogin1.php?
$ Name = $ _ POST ['name'];?
$ Pass = $ _ POST ['pass'];?
If (! $ Name? | ?! $ Pass )? {?
???? Echo? "The user name or password is blank. please log on again ";?
???? Die ();?
}?
If? (! ($ Name = "laogong "? &&? $ Pass = "123 ")? {?
???? Echo? "The user name or password is incorrect. please log on again ";?
???? 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 );?
// If the authentication is successful, what operations can be performed?
Echo? "Logged on
";?
Echo? "Next page ";?
?>?
Mylogin2.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 to perform related operations?
???? Echo? "Not logged in, no access permission ";?
???? Echo? "Please log on and browse ";?
???? Die ();?
}?
?>?
Similarly, disable the cookie test. Username: laogong? Password: 123 ?? Is this session stored through a file? Id. the file is: e :? Mpphpsid.txt. determine 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 on the previous page? Id, and then try to pass it to the next page, and add the code session_id before the code (the passed session? Id );?