php 怎么实现七天免登录
php实现七天免登录的方法:1、在前端创建一个用户选择七天免登录的按钮;2、在后端中,根据用户提交的用户名和密码查询到用户的id;3、将用户id存入cooike中;4、设置七天的过期时间即可。

php入门到就业线上直播课:进入学习
Apipost = Postman + Swagger + Mock + Jmeter 超好用的API调试工具:点击使用
本教程操作环境:windows7系统、PHP8.1版、Dell G3电脑。
php 怎么实现七天免登录?
php中实现7天免登录功能,防止cookie欺骗
1、免登录思路
用户选择七天免登录按钮,后端根据用户提交的用户名和密码查询到用户的id将用户id存入cooike中并设置七天的过期时间。在不清除cookie信息(非正常退出的时候),后台帮助用户登录。实际就是利用cooki实现。
2、验证登录文件:checkLogin.php
<?php
header('content-type:text/html;charset=utf-8');
require './config.php';
$username = $_POST['uname'];
$password = md5($_POST['pwd']);
$islogin = $_POST['islogin'];
$sql = "SELECT * FROM `mu_user` WHERE `username`=? AND `password`=? ";
$stm = $pdo -> prepare($sql);
$stm ->bindParam(1,$username);
$stm ->bindParam(2,$password);
$stm ->execute();
$res = $stm->fetch(PDO::FETCH_ASSOC);
if($stm->rowCount() == 1){
//验证成功
clearCookie();
if($islogin==1){
//记住密码
setcookie("username",$res['username'],strtotime('+7 days'));
$token = settoken($res['username'],$res['password'],$res['id']);
setcookie("token",$token,strtotime('+7 days'));
}else{
// 无记住密码
setcookie("username",$res['username']);
$token = settoken($res['username'],$res['password'],$res['id']);
setcookie("token",$token);
}
exit("
<script>
alert('登录成功!');
location.href ='index.php';
</script>
");
}else{
//验证失败
exit("
<script>
alert('用户名或密码有误!');
location.href ='login.php';
</script>
");
}
//清除cookie
function clearCookie(){
setcookie("username",'',time()-1800);
setcookie("token",'',time()-1800);
}
//设置token
function settoken($username,$password,$id)
{
$salk = "czx";
$token = md5($salk.$username.$password)."*".$id;
return $token;
}
登录后复制

