PHP会话(COOKIE和SESSION):注册登陆实操案例
程序员文章站
2022-03-01 17:28:02
...
PHP会话基础知识:
会话方式——COOKIE
1.设置cookie值:setcookie("name","value",time()+时间,"路径","域名");
2.读取:$_COOKIE["name"]
中的值
3.关闭(销毁会话):通过过设置过期时间来setcookie("name","value",time()-时间,"路径","域名");
会话方式二SESSION
1.开启会话:session_start();
- 任何关于SESSION的操作都必须开启会话
2.把信息写入SESSION中:$_SESSION["key"]="value";
3.读取SESSION的中的值$_SESSION["key"]
4.关闭(销毁)会话
- 销毁SESSION中的某个值
unset($_SESSION["key"]);
- 销毁SESSION中的所有值
session_unset();
- 销毁SESSION文件:
session_destroy();
注册登陆案例实战
(一)后台登陆注册以及退出操作
<?php
// echo json_encode($_POST);
session_start();//开启会话
// echo json_encode($_GET["active"]);
include('connection.php');//连接数据库
$active=$_GET["active"];//获取操作方式(登陆?注册?退出)
// 判断并执行
if($active=="register"){
$sql="insert users (username,email,password) values (?,?,?);";
$stmt=$pdo->prepare($sql);
$stmt->execute([$_POST["username"],$_POST['email'],$_POST["password"]]);
// $stmt->debugDumpParams();
if($stmt->rowCount()>0){
$_SESSION['name']=$_POST["username"];
$_SESSION["username"]=$_POST['email'];
$_SESSION["password"]=$_POST['password'];
echo json_encode([$pdo->lastInsertId(),"注册成功"]);
}else{
echo json_encode([0,"注册失败"]);
}
}elseif($active=="login"){
$sql="select username,email,password from users where email=?";
$stmt=$pdo->prepare($sql);
$stmt->execute([$_POST["email"]]);
$res=$stmt->fetch();
if($res['email']==$_POST["email"]&&$res["password"]==$_POST["password"]){
$_SESSION["name"]=$res['username'];
$_SESSION["username"]=$_POST['email'];
$_SESSION["password"]=$_POST['password'];
echo json_encode([$res["username"],"登陆成功"]);
}else{
echo json_encode([0,"登陆失败"]);
}
}elseif($active=="logout"){
// session_destroy();
unset($_SESSION["username"]);
exit('<script>alert("你已经退出!"); window.location.href="home.php";</script>');
}else{
echo "非法操作";
}
(二)前端登陆和注册
1.登陆
<?php
session_start();
$username=$_SESSION["username"];
if($username){
exit('<script>alert("你已经登陆!"); window.location.href="home.php";</script>');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<title>登陆</title>
</head>
<style>
* {
margin: 0;
padding: 0;
}
form {
width: 400px;
margin: 0 auto;
display: flex;
flex-flow: column nowrap;
align-items: center;
}
form * {
width: 360px;
height: 28px;
margin-top: 5px;
}
</style>
<body>
<form action="" method="post" name="login">
<h3>用户登陆</h3>
<input type="email" name="email" placeholder="请输入邮箱账号">
<input type="password" name="pwd" placeholder="请输入密码">
<button type="button">登陆</button>
<div class="tips"></div>
</form>
</body>
<script>
$("[type='button']").click(function() {
email = $("[name='email']").val().trim();
pwd = $("[name='pwd']").val().trim();
if (email.length > 0) {
if (pwd.length > 0) {
$.ajax({
type:'POST',
url:'http://php.edu/test/handle.php?active=login',
data:{
"email":email,
"password":pwd
},
dataType:"json",
success:function(res){
if(res[0]) {
$('.tips').append("<span>登陆成功,跳转中……</span>");
setTimeout(() => {
window.location.href="home.php";
}, 1000);
}else{
alert("账号密码错误!!!");
$('input').val('');
}
}
});
} else {
alert("密码不能为空");
return false;
}
} else {
alert("邮箱不能为空");
return false;
}
})
</script>
</html>
2.注册
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<title>注册</title>
</head>
<style>
* {
margin: 0;
padding: 0;
}
form {
width: 400px;
margin: 0 auto;
display: flex;
flex-flow: column nowrap;
align-items: center;
}
form * {
width: 360px;
height: 28px;
margin-top: 5px;
}
</style>
<body>
<form action="" method="post" name="register">
<h3>用户注册</h3>
<input type="text" name="username" placeholder="用户名">
<input type="email" name="email" placeholder="XXXX@163.com">
<input type="password" name="pwd1" placeholder="不少于6位">
<input type="password" name="pwd2" placeholder="再次确认密码">
<button type="button">注册</button>
<div class="tips"></div>
</form>
</body>
<script>
$("[type='button']").click(function() {
username = $("[name='username']").val().trim();
email = $("[name='email']").val().trim();
pwd1 = $("[name='pwd1']").val().trim();
pwd2 = $("[name='pwd2']").val().trim();
console.log(pwd1,pwd2);
if (username.length > 0) {
if (email.length > 0) {
if (pwd1 == pwd2 && pwd1.length > 0) {
$.ajax({
type:'POST',
url:'http://php.edu/test/handle.php?active=register',
data:{
"username":username,
"email":email,
"password":pwd1
},
dataType:"json",
success:function(res){
if(res[0]) {
$('.tips').append("<span>登陆成功,跳转中……</span>");
setTimeout(() => {
window.location.href="home.php";
}, 1000);
}else{
alert("注册失败!!!请重注册");
$('input').val('');
}
}
});
} else {
alert("密码不能为空,且两密码必须一致");
return false;
}
} else {
alert("邮箱不能为空");
return false;
}
} else {
alert("用户名不能为空");
return false;
}
})
</script>
</html>
(三)首页判断是否登陆并显示信息
<?php
session_start();
$user=$_SESSION["username"];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>首页</title>
</head>
<style>
*{
margin:0;
padding:0;
}
header{
height:40px;
background-color:lightgray;
display:flex;
justify-content: space-between;
padding:0 20px;
align-items:center;
}
header a {
text-decoration: none;
color:black;
}
</style>
<body>
<header>
<a href="<?= $_SERVER["SCRIPT_NAME"] ?>">首页</a>
<div>
<?php if($user): ?>
<a href=""><?= $user?></a>
<a href="http://php.edu/test/handle.php?active=logout">退出</a>
<?php else:?>
<a href="http://php.edu/test/login.php">登陆</a>
<a href="http://php.edu/test/register.php">注册</a>
<?php endif ?>
</div>
</header>
<?php
include 'index.php';
?>
</body>
</html>
(四)运行效果
上一篇: Compression
下一篇: docker常用命令,我与你一起实战演练