Friends who have used session in PHP may encounter such a problem, and the session variable cannot be passed across page. This makes me feel a lot of days, and I finally think about it and solve this problem. I think that the reason for this problem has the following points: 1, the client is banned with cookie 2, the browser has problems, temporarily unable to access the cookie 3, the session.use_trans_sid = 0 in php.ini is not opened when compiled - -ENABLE-Trans-SID option
Why is this so? Let me explain it:
SESSION is stored in the server side (default in file mode storage session), obtain the user's file, obtain the value of the variable, the session ID can use the client's cookie or HTTP1.1 protocol Query_String (Accessible) The "?" "?" Of the URL is transmitted to the server, and then the server reads the session directory ... That is, the Session ID is an ID card that acquires the session variable stored on the service. When the code session_start (); running, a session file is generated on the server, and there is also a SESSION ID that is unique, defining the session variable in a certain form of SESSION files you have just generated. With the session ID, you can take out the defined variables. After cross-page, in order to use session, you must perform session_start (); will generate a session file, with the corresponding session ID, with this session ID, is not a first session file mentioned earlier The variables of this session ID are not turning its "key". If you are in session_start (); before adding code session_id ($ session id); will not generate a new session file, directly read the session file corresponding to this ID.
The session in PHP is by default, using the client's cookie to save the session ID, so it will affect the session when the client's cookie occurs. It must be noted that session does not necessarily depend on cookie, which is also the SESSION compared to cookie's high. When the client's cookie is disabled or a problem, PHP automatically attaches the session ID in the URL, so that you can use the session variable across the page through the session ID. However, this adhesion is also a condition, ie "session.use_trans_sid = 1 in php.ini, opens up the --enable-trans-sid option" when compiled.
I have known the forum. When I enter the forum, I will tend to check if cookies open, because most forums are based on cookies, forums use it to save username, password and other user information, easy to use . And many friends think that cookie is not safe (in fact, not this), often disabled. In fact, in the PHP program, we can use the session instead of cookies, which can do not depend on whether the client is open to cookie.
So, we can leave the cookie using the session, which means that the user shuts down the cookie, using sessions, and the current path has the following:
1. Set session.use_trans_sid = 1 in php.ini or to turn on the -enable-trans-sid option when compiling, let PHP automatically pass the session ID. 2, manually pass the URL pass value, hide the form to pass the session ID. 3, save the session_id in the form of files, databases, manually calling during the cross page. Way 1 Example:
S1.php
php session_start (); $ _SESSION ['var1'] = "People's Republic of China"; $ URL = " Next page "; Echo $ URL ;?>
S2.php
PHP session_start (); echo "The value of the session variable VAR1 is:" $ _ session ['var1'];?>
Run the above code, in the case of the client cookie, you should be able to get the result "People's Republic". Now you manually turn off the client's cookie, run again, may not get the result. If you don't get the result, "Set the session.use_trans_sid = 1 in php.ini to open the --enable-trans-sid option", and get the results of the "People's Republic"
Way 2 Example:
S1.php
php session_start (); $ _SESSION ['var1'] = "People's Republic of China"; $ SN = session_id (); $ URL = " Next page "; Echo $ URL;?>>
S2.php
PHP session_id ($ _ get ['s']); session_start (); echo "The value of the session variable VAR1 is:" $ _ session ['var1'];?>
The method of hiding the form is basically the same.
Way 3 Example:
Login.html
MyLogin1.php
PHP
$ Name = $ _ post ['name']; $ pass = $ _ post ['pass']; if (! $ name ||! $ pass) {echo "username or password is empty, please Re-login "; die ();} if (! ($ name == "123") {echo "username or password is incorrect, please HREF = "login.html"> Re-login "; Die ();} // Registered user ob_start (); session_start (); $ _SESSION ['user'] = $ name; $ psid = session_id () ; $ fp = fopen ("e: /tmp/phpsid.txt", "w "; FWRITE ($ fp, $ psid); fclose ($ fp); // Authentication success, performing Echo "has been logged in < Br> "; echo" Next ";
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 in!";} Else {// Successfully logged in to perform related operations ECHO "Not logged in, no right to access"; echo "Please Log in After browsing"; DIE ();
?>
Similarly, please close the cookie test, username: YOUNGONG password: 123 This is to save the session ID through the file, the file is: E: MPPHPSID.TXT, please determine the file name or path according to your own system.
As for the method of using a database, I don't give an example, similar to the document method.
Summarize, the above method has a common point, that is, get the session ID in the previous page, then find a way to pass to the next page, on the next page session_start (); before the code session_id (passing the session ID);