Home >Backend Development >PHP Tutorial >PHP realizes automatic login by remembering password_PHP tutorial
To realize the function of remembering passwords and logging in automatically, most of our data is implemented by using cookies on the client side. Our use of PHP is no exception. Friends in need can refer to it.
The solution for PHP to remember passwords and automatically log in is actually the operation of sessions and cookies
//Check whether the user is logged in
The code is as follows
|
Copy code |
||||||||
function checklogin(){ if(empty($_SESSION ['user_info'])){ //Check whether the session is empty
header("location:login.php?req_url=".$_SERVER['REQUEST_URI']); //Go to the login page, record the requested url, and jump after login Turn over, the user experience is good. if(empty($user)){ //The username and password are incorrect and the information is not retrieved. Go to the login page
| } }
The code is as follows | Copy code | ||||
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 = "Verification code is incorrect"; }elseif($username=='' || $password==''){ $err_msg = "Neither username nor password can be empty"; }else{ $row = getUserInfo($ username,$password); if(empty($row)){ $err_msg = "Both the username and password are incorrect"; }else{ $_SESSION['user_info '] = $row; if(!empty($remember)){ //If the user chooses, record the login status and put the username and encrypted password 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"); } } } Third, when the user clicks to exit, clear the recorded login status //Exit Login
|