Home >Backend Development >PHP Tutorial >PHP uses cookies to remember login status_PHP tutorial

PHP uses cookies to remember login status_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:55:58873browse

php uses cookies to remember login status

This article mainly introduces php to use cookies to remember login status. This article uses the most original method to explain how to remember login. Status, 3 steps and specific implementation code are given. Friends in need can refer to it

To realize the function of remembering passwords and logging in automatically, most of our data is achieved by using cookies on the client side. We use PHP as well. 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

1. Check whether the user is logged in

The code is as follows:


//Check if the user is logged in
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 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.
}else{ //The user has chosen to remember 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 has not been retrieved. Go to the login page
header("location:login.php?req_url=".$_SERVER['REQUEST_URI']);
}else{
$_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

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 = "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;
?>

type="password" name="pwd" id="pwd" value="" /> name="remember" type="checkbox" value="1"
checked="checked" /> Remember me! id="button" value="Login" />

//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);
}
}
?>

www.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...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn