if(empty($_COOKIE['username']) || empty($_COOKIE['password'])){ //If the session is empty and the user does 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, jump to it after logging in, and have a good user experience.
$user = getUserInfo($_COOKIE['username'],$_COOKIE['password']); //Get the user's personal information
if(empty($user)){ //The username and password are incorrect and the information has not been retrieved. Go to the login page
$_SESSION['user_info'] = $user; //If the username and password are correct, put the user's personal information into the session
2. User submits login information
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 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");
}
}
}
3. When the user clicks to exit, clear the recorded login status
The code is as follows:
//Log out
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);
}
}
4. Simple version example
The code is as follows:
//Read the username and password values of COOKIE
if($_COOKIE['uname']!=''){$CKUNAME = $_COOKIE['uname'];}
if($_COOKIE['pwd']!=''){$CKPWD = $_COOKIE['pwd'];}
echo $CKUNAME;
echo '
';
echo $CKPWD;
?>
//Log in and save username and password to COOKIE
if($_POST['button']!=''){
$uname = $_POST['uname'];
$pwd = $_POST['pwd'];
//If the entered encryption password is different from the COOKIE, then encrypt it
if($pwd!=$CKPWD){$pwd = md5($pwd);}
$remember = $_POST['remember'];
if($remember==1){
setcookie("uname", $uname, time() 3600*24*30);
setcookie("pwd", $pwd, time() 3600*24*30);
}
}
?>
http://www.bkjia.com/PHPjc/990988.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/990988.htmlTechArticlephp uses cookies to remember login status. This article mainly introduces php to use cookies to remember login status. This article Use the most primitive method to explain how to remember the login status, giving 3 steps...