A solution to the problem of a session in PHP that cannot be passed across pages
A friend who has used a session in PHP may encounter a problem where session variables cannot be passed across pages. It upset me for a few days, and I finally thought through the data and solved the problem. I think that there are several reasons for this problem:?
1. Cookies are disabled by the client?
2, browser problems, temporarily unable to access cookies?
3, session.use_trans_sid?=?0 in php.ini or not open--enable-trans-sid option at compile time?
Why is that? Let me explain:?
The session is stored on the server side (the session is stored by default as a file), the user's file is obtained according to the Session?id provided by the client, the value of the variable is acquired, Session?id can use the client's cookie or the Http1.1 protocol Query_ String (the "?" of the URL that is visited Later) to the server, and then the server reads the Session Directory .... That is, Session?id is the identity card that gets the session variable stored on the service. When the Code session_start () runs, it creates a session file on the server, and the only corresponding Session?id is generated, defining the session variable to be stored in a certain form in the session file that was just generated. By Session?id, you can remove a defined variable. After the spread, in order to use the session, you must also execute session_start (); A session file will be generated, corresponding to the corresponding session?id, with this session?id is not the variable in the first session file mentioned earlier, because this session?id is not the "key" that opens it. If the Session_Start (), preceded by the 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 uses the client's cookie to save the Session?id by default, so it will affect the session when there is a problem with the client's cookie. It is important to note that the session does not necessarily have to rely on cookies, which is a clever place for the session compared to cookies. When a client's cookie 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 by Session?id. However, this attachment also has certain conditions, that is, "php.ini in the session.use_trans_sid?=?1 or compile-time open open the--enable-trans-sid option."?
Friends who have used the forum know that when entering the forum, you will often be prompted to check whether the cookie is open, this is because most of the forum is based on cookies, forum use it to save user name, password and other user information, easy to use. And many friends think that cookies are unsafe (not really) and often disable them. In fact, in the PHP program, we can completely replace the cookie with the session, it can not rely on the client whether to open cookies.
So, we can put aside cookies using the session, that is, assuming that the user closed the cookie in the case of the use of the session, there are several ways to achieve the following:?
1, set the php.ini in the session.use_trans_sid?=?1 or the compile-time open the--ENABLE-TRANS-SID option, let PHP automatically cross-page delivery session?id.?
2, manually through the URL value, hidden form delivery session?id.
3, file, database and other forms to save session_id, in the process of cross-page calls manually.
Route 1 illustrate:?
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, in case the client cookie is normal, should be able to get the result "People's Republic of China".
Now you manually shut down the client's cookie and run it, you may not get the result. If you don't get the results, then "set session.use_trans_sid?=?1 in php.ini or open--enable-trans-sid option at compile time" and get the result "People's Republic of China"?
Route 2 illustrate:?
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 basics of how to hide a form are the same.
Route 3 illustrate:?
Login.html?
?
?
?
<title>Login</title>?
?
?
?
Please login:?
?
?
?
Mylogin1.php?
$name =$_post[' name '];?
$pass =$_post[' pass ';?
if (! $name? | |?! $pass)? {?
???? Echo? " The user name or password is blank, please login again ";?
???? Die ();?
}?
If? (! ($name = = "Laogong"? &&? $pass = = "123")? {?
???? Echo? " The user name or password is incorrect, please log in again.
???? Die ();?
}?
Registered users?
Ob_start ();?
Session_Start ();?
$_session[' user ']=? $name;?
$psid =session_id ();?
$FP =fopen ("E:\tmp\phpsid.txt", "w+";?
Fwrite ($fp, $psid);?
Fclose ($FP);?
Successful authentication, related operations?
Echo? " Is logged in
";?
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? " Already logged in! ";?
}?
Else? {?
???? Successful login for related operations?
???? Echo? " Not logged in, not authorized to access ";?
???? Echo? " Please log in after browsing ";?
???? Die ();?
}?
?
Also please turn off the cookie test, user name: Laogong? password: 123?? This is saved through the file Session?id, the file is: E:?mpphpsid.txt, according to your system to determine the file name or path.
As for the method of using the database, I will not give an example, similar to the method of file.
Summing up, the above method has a common point, is to get session?id on the previous page, and then try to pass to the next page, the next page of the Session_Start (), code before the code session_id (pass over the session?id);