Home > Backend Development > PHP Tutorial > PHP uses cookies to remember login status code examples

PHP uses cookies to remember login status code examples

怪我咯
Release: 2023-03-13 10:54:01
Original
1506 people have browsed it

This article mainly introduces how PHP uses cookie to remember the login status. This article uses the most original method to explain how to remember the login status, giving 3 steps and specific implementation code. What is needed Friends can refer to

To 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 as well. Friends in need can refer to it.

php's solution to remembering passwords and automatically logging in is actually the operation of session, cookies

1. 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 after login

Second, the 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 = "验证码不正确";  
}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");  
}  
}  
}
Copy after login

Three, when the user clicks to exit , clear the logged in status

The code is as follows:

//退出登录 
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);  
}  
}
Copy after login

Four, concise Version example

The code is as follows:

<?
//读取COOKIE的用户名和密码的值即可
if($_COOKIE[&#39;uname&#39;]!=&#39;&#39;){$CKUNAME =  $_COOKIE[&#39;uname&#39;];}
if($_COOKIE[&#39;pwd&#39;]!=&#39;&#39;){$CKPWD =  $_COOKIE[&#39;pwd&#39;];}
echo $CKUNAME;
echo &#39;<br>&#39;;
echo $CKPWD;
?>
<form id="form1" name="form1" method="post" action="">
    <input type="text" name="uname" id="uname" value="<?=$CKUNAME;?>" /> <input
       type="password" name="pwd" id="pwd" value="<?=$CKPWD;?>" /> <input
       name="remember" type="checkbox" value="1" <? if($CKUNAME!=&#39;&#39;){?>
       checked="checked" <? } ?> /> 记住我! <input type="submit" name="button"
       id="button" value="登录" />
</form>
<?
//登录,将用户名和密码存入到COOKIE
if($_POST[&#39;button&#39;]!=&#39;&#39;){
    $uname = $_POST[&#39;uname&#39;];
    $pwd = $_POST[&#39;pwd&#39;];
    //如果输入的加密密码和COOKIE中不一样,那么就加密
    if($pwd!=$CKPWD){$pwd = md5($pwd);}
    $remember = $_POST[&#39;remember&#39;];
    if($remember==1){
       setcookie("uname", $uname, time()+3600*24*30);
       setcookie("pwd", $pwd, time()+3600*24*30);
    }
}
?>
Copy after login

The above is the detailed content of PHP uses cookies to remember login status code examples. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template