Php implements automatic login by remembering the password. To implement the function of remembering automatic password logon, most of our data is implemented using cookies on the client. We use php, which is no exception. if you need it, please refer to it. Php is designed to implement the function of remembering automatic password logon. most of our data is implemented using cookies on the client side. We use php, which is no exception. if you need it, please refer to it.
Php creates a solution for remembering the password for automatic login, that is, to operate sessions and cookies.
// Check whether the user logs on
The code is as follows: |
|
Function checklogin (){ If (empty ($ _ SESSION ['User _ info']) {// check whether the session is empty. If (empty ($ _ COOKIE ['username']) | empty ($ _ COOKIE ['password']) {// if the session is empty, the user does not select to log on. Header ("location: login. php? Req_url = ". $ _ SERVER ['request _ URI ']); // go to the logon page and record the REQUEST url. after logging on, go to the logon page. the user experience is good. } Else {// Remember the logon status $ User = getUserInfo ($ _ COOKIE ['username'], $ _ COOKIE ['password']); // retrieves the user's personal data If (empty ($ user) {// if the user name and password are incorrect, the information is not obtained. go to the logon page. Header ("location: login. php? Req_url = ". $ _ SERVER ['request _ URI ']); } Else { $ _ SESSION ['User _ info'] = $ user; // The user name and password are correct. put the user's personal data in the session. } } } } |
2. the user submits logon information
The code is as follows: |
|
Username = trim ($ _ POST ['username']); $ Password = md5 (trim ($ _ POST ['password']); $ Validatecode = $ _ POST ['validatecode']; $ Ref_url = $ _ GET ['req _ url']; $ Remember = $ _ POST ['remember']; $ Err_msg = ''; If ($ validatecode! = $ _ SESSION ['checksum']) { $ Err_msg = "incorrect verification code "; } Elseif ($ username = ''| $ password = ''){ $ Err_msg = "neither user name nor password can be blank "; } Else { $ Row = getUserInfo ($ username, $ password ); If (empty ($ row )){ $ Err_msg = "the user name and password are incorrect "; } Else { $ _ SESSION ['User _ info'] = $ row; If (! Empty ($ remember) {// if the user chooses to log on, the user name and password added are recorded in the cookie. Setcookie ("username", $ username, time () + 3600*24*365 ); Setcookie ("password", $ password, time () + 3600*24*365 ); } If (strpos ($ ref_url, "login. php") === false ){ Header ("location:". $ ref_url ); } Else { Header ("location: main_user.php "); } } } |
3. when the user points out, the logon status is cleared.
// Log out
The code is as follows: |
|
Function logout (){ Unset ($ _ SESSION ['User _ info']); If (! Empty ($ _ COOKIE ['username']) |! Empty ($ _ COOKIE ['password']) { Setcookie ("username", null, time ()-3600*24*365 ); Setcookie ("password", null, time ()-3600*24*365 ); } } |
Bytes. Php production...