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 代码如下 | 复制代码 | function checklogin(){ if(empty($_SESSION['user_info'])){ //检查一下session是不是为空 if(empty($_COOKIE['username']) || empty($_COOKIE['password'])){ //如果session为空,并且用户没有选择记录登录状 header("location:login.php?req_url=".$_SERVER['REQUEST_URI']); //转到登录页面,记录请求的url,登录后跳转过去,用户体验好。 }else{ //用户选择了记住登录状态 $user = getUserInfo($_COOKIE['username'],$_COOKIE['password']); //去取用户的个人资料 if(empty($user)){ //用户名密码不对没到取到信息,转到登录页面 header("location:login.php?req_url=".$_SERVER['REQUEST_URI']); }else{ $_SESSION['user_info'] = $user; //用户名和密码对了,把用户的个人资料放到session里面 } } } } | |
Copy code
|
function checklogin(){ if(empty($_SESSION ['user_info'])){ //Check whether the session is empty 代码如下 | 复制代码 | 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 = "验证码不正确"; }elseif($username=='' || $password==''){ $err_msg = "用户名和密码都不能为空"; }else{ $row = getUserInfo($username,$password); if(empty($row)){ $err_msg = "用户名和密码都不正确"; }else{ $_SESSION['user_info'] = $row; if(!empty($remember)){ //如果用户选择了,记录登录状态就把用户名和加了密的密码放到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"); } } } | if(empty($_COOKIE['username']) || empty($_COOKIE['password'])){ //If session Is empty, and the user did not choose to record the login status 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. }else{ //The user has chosen to remember the login status $user = getUserInfo($_COOKIE['username'],$_COOKIE['password']); // Get the user's personal information if(empty($user)){ //The username and password are incorrect and the information is not retrieved. Go to the login page header("location:login.php?req_url=".$_SERVER['REQUEST_URI']) ; }else{ 代码如下 | 复制代码 | 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); } } | $_SESSION['user_info'] = $user; //The username and password are correct, put the user’s personal information in the session | } }
} }
} } Second, the user submits login information
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 The code is as follows |
Copy code |
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); } }
http://www.bkjia.com/PHPjc/444728.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/444728.htmlTechArticleTo realize the function of remembering passwords and automatically logging in, most of our data is realized by using cookies on the client side. We use PHP is no exception, friends in need can refer to it. php production...
|
|