The php implementation remembers that there are more than one automatic Password Logon method. The following shows two emptyempty methods, which are actually one, because the code is highlighted with a bug. I hope to help you.
1. User Logon check
Copy codeThe Code is as follows: // check whether the user logs on
Function checklogin (){
If (emptyempty ($ _ SESSION ['user _ info']) {// check whether the session is empty.
If (emptyempty ($ _ COOKIE ['username']) | emptyempty ($ _ 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 login page, record the REQUEST url, and jump to the past after logon. the user experience is good.
} Else {// remember the logon status
$ User = getUserInfo ($ _ COOKIE ['username'], $ _ COOKIE ['Password']); // retrieves the user's personal data
If (emptyempty ($ user) {// the user name and password are incorrect and cannot be 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.
}
}
}
}
// Check whether the user logs on
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 login page, record the REQUEST url, and jump to the past after logon. 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.
}
}
}
}
Perform the preceding check before accessing every page in the background.
2. the user submits logon information
Submit the user name and password here,Copy codeThe 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 (emptyempty ($ row )){
$ Err_msg = "the user name and password are incorrect ";
} Else {
$ _ SESSION ['user _ info'] = $ row;
If (! Emptyempty ($ remember) {// if the user chooses, record the logon status and put the user name and password encrypted into 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 ");
}
}
}
$ 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 ");
}
}
}
For A brief explanation of $ ref_url, assume that user A accesses user B. php, but user A does not log on, jump to the login page login. php. after entering the user and password on the logon page, confirm and redirect to B. php page, instead of redirecting to a default page main_user.php. Because B. php is the page that user A wants to visit, the user experience will be better.
3. When the user points out, the logon status is cleared.
Why do you want to do this? If someone else uses your computer, they may browse your personal privacy. So when the user deliberately quits, it is best to cancel the log logon status.Copy codeThe Code is as follows: // log out
Function logout (){
Unset ($ _ SESSION ['user _ info']);
If (! Emptyempty ($ _ COOKIE ['username']) | emptyempty ($ _ COOKIE ['Password']) {
Setcookie ("username", null, time ()-3600*24*365 );
Setcookie ("password", null, time ()-3600*24*365 );
}
}