Let's talk about using session to log on. The 05php code on the logon page is as follows: & lt ;? Phpsession_start (); initialize sessionif (isset ($ _ SESSION [& amp; 39; shili & amp; 39;
Let's talk about the use of session to log on. The Code of 05. php on the logon page is as follows:
-
- If (isset ($ _ SESSION ['shili']) {
- Header ("Location: shili. php"); // redirect to another page
- Exit ;}?>
-
- Function checklogin (){
- If (login. username. value! = "") & (Login. password. value! = ""))
- Return true // if the user name and password are not empty, TRUE is returned.
- Else {alert ("nickname or password cannot be blank! ")}}
- Script
-
- . Style1 {font-size: 13px; font-family: ""; font-weight: normal; color: # 0099FF ;}
-
- "Center">
-
-
-
-
This program is an administrator logon interface. First, it initializes the session and then checks whether the session variable has been registered to check whether the user has logged on. if the user has logged on, you do not need to log on again, direct to other pages. Javascript is also used to determine whether the user name and password are entered.
Verify the logon page,06. php code:
-
- If (isset ($ _ SESSION ['shili']) {
- Header ("Location: shili. php"); // redirect to another page
- Exit;} // end immediately after logon
- $ Shili_name = $ _ POST ['username']; // Get the parameter
- $ Password = $ _ POST ['password'];
- // Verify that the administrator name and password are correct. verify the password directly and no database connection is available.
- If ($ shili_name = "mr" and $ password = "mrsoft "){
- Session_register ("shili"); // register a new variable and save the nickname of the current session
- $ Shili = $ shili_name;
- Echo "logon successful! ";
- Header ("Location: shili. php"); // redirect to the management page after successful logon
- } Else {
- Echo" ";
- Echo "incorrect account or password, or not administrator account
";
- Echo "logon failed!
Enter ";
- Echo"";}
- ?>
The program first checks whether the user has logged on through the session variable. if not, the user name and password entered must be verified. if the user name and password are correct, the session variable is registered and the logon is successful !. If not, the system prompts logon failure.
This is a simple program for user login verification. the session variable prevents users who directly browse without logon. If you want to restrict the browsing of a page, you can use the same method, as long as you put the following code at the beginning of the page:
-
- If (! Isset ($ _ SESSION ['shili']) {
- Echo" ";
- Echo"";
- Echo "you have not logged on. please log on! ";
- Echo"";
- Exit ();}
- ?>
Share login using session_id
We should be able to understand the problem of sharing sessions among multiple servers. when the number of users on a website is too large, a server cluster will be used, for example, a dedicated login server. After a user logs on to the server, the login server saves the user's login information session, while other accessed servers, such as the movie server does not have this session, we need to share this session through a unique identifier of the session. the sharing of a specific session is beyond the scope of this article. please refer to the relevant materials on your own.
The second purpose is to verify different sessions of the same user, which is hard to understand. In this case, when a user requests data not through a browser but through a socket or other method, we must first perform user login verification on the user. after the verification is successful, send a sessionid to him, and then carry this sessionid every time he requests it. we can use this sessionid to determine whether the session already exists. if so, we can determine that the user has logged on ......
For the first problem, we can save the sessionid in the database for implementation. this method is safe and widely used, but it is not the scope of our discussion. first, a sessionid is generated during verification; the code is as follows:
-
- Session_start ();
- $ SessionId = session_id (); // Obtain the sessionid.
- // Send the session to the client
- .........
- ?>
The client carries the sessionid variable to request data. the code is as follows:
-
- Session_id ('$ sessionid'); // note that this function includes parameters.
- Session_start (); // This function must be after session_id ()
- ?>
Good, no problem. you have got a good solution. if you need to know about it, please refer to it.