This article describes in detail the common background management Logon of sessions and the method of using session IDs to achieve user sharing logon between multiple servers. If you need to know about this, you can view the entire article.
Let's talk about using session to log on.
The code for Logon page 05. php is as follows:
The Code is as follows: |
Copy code |
<? Php session_start (); // initialize the session If (isset ($ _ SESSION ['shili']) { Header ("Location: shili. php"); // redirect to another page Exit ;}?> <Script language = "javascript"> 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> <Style type = "text/css"> . Style1 {font-size: 13px; font-family: ""; font-weight: normal; color: # 0099FF ;} </Style> <Div align = "center"> <Table width = "260" border = "1" bgcolor = "# D8EFFA"> <Form name = "login" method = "post" action = "06.php" onSubmit =" return checklogin () "> <Tr align = "center"> <Td height = "30" colspan = "2"> <span class = "style1"> Management System logon </span> </td> </Tr> <Tr> <Td width = "90" align = "center" class = "style1"> administrator: </td> <Td width = "170" height = "20" align = "left" valign = "middle"> <input name = "username" type = "text" id = "username" size = "20"> </td> </Tr> <Tr> <Td align = "center" class = "style1"> password: </td> <Td height = "20" align = "left" valign = "middle"> <input name = "password" type = "password" id = "password" size = "20"> </td> </Tr> <Tr> <Td align = "center" class = "style1"> & nbsp; </td> <Td height = "20" align = "center"> <input type = "submit" name = "Submit" value = ""> </td> </Tr> </Form> </Table> </Div> |
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.
Login verification page
06. php code:
The Code is as follows: |
Copy code |
<? Php session_start (); // initial session 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 "<font color = red> logon successful! </Font> "; Header ("Location: shili. php"); // redirect to the Management page after Successful Logon } Else { Echo "<table width = '000000' align = center> <tr> <td align = center> "; Echo "incorrect account or password, or not an administrator account <br> "; Echo "<font color = red> logon failed! </Font> <br> <a href = 'HTTP: // wyl072.blog.163.com/blog/05.php'> enter a new one </a> "; Echo "</td> </tr> </table> ";} ?> |
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:
The Code is as follows: |
Copy code |
<? Php session_start (); If (! Isset ($ _ SESSION ['shili']) { Echo "<p align = center> "; Echo "<font color = # ff0000 size = 5> <strong> <big> "; Echo "You have not logged on. Please <a href = 'HTTP: // wyl072.blog.163.com/blog/denglu.php'> log on </a>! "; Echo "</big> </strong> </font> </p> "; Exit ();} ?> |
Share login using session_id
First, 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 question, 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: |
Copy code |
<? Php 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: |
Copy code |
<? Php 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.