index.php 入口文件
<?php
namespace login;
// 开启会话
session_start();
// 判断是否已登录?
if (isset($_SESSION['user'])) {
// 反序列化
$user = unserialize($_SESSION['user']);
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<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>
<style>
nav {
height: 40px;
background-color: deepskyblue;
padding: 0 20px;
display: flex;
justify-content: space-between;
align-items: center;
}
nav .loginbox {
display: flex;
padding: 0 20px;
justify-content: space-between;
align-items: center;
gap: 0px 20px;
}
nav .loginbox>a,
nav .loginbox>span {
color: white;
text-decoration: none;
}
nav .loginbox>span {
font-weight: bold;
}
nav .loginbox>a:hover {
cursor: pointer;
}
</style>
</head>
<body>
<nav>
<a href="index.php">我的博客</a>
<div class="loginbox">
<?php if (isset($user)) : ?>
<span>用户昵称: <?= $user[0]['name'] ?></span>
<span>用户性别: <?= $user[0]['sex'] ? '女' : '男' ?></span>
<a id="logout">退出</a>
<?php else : ?>
<a href="login.php">登录</a>
<?php endif ?>
</div>
</nav>
<script>
// 为退出按钮创建事件监听器
document.querySelector('#logout').addEventListener('click', function(event) {
if (confirm('是否退出')) {
// 禁用默认行为, 其实就是禁用原<a>标签的点击跳转行为,使用事件中的自定义方法处理
event.preventDefault();
// 跳转到退出事件处理器
window.location.assign('handle.php?action=logout');
}
});
</script>
</body>
</html>
login.php 登录界面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用户登录</title>
<style>
body {
background: linear-gradient(100deg, white, #00d5ff)
}
fieldset {
width: 300px;
background: linear-gradient(100deg, #00d5ff, #00aaff);
margin: 200px auto;
}
legend {
background-color: white;
margin: auto;
font-size: larger;
}
fieldset>div {
width: 240px;
margin: auto;
padding: 10px;
}
fieldset>div form div {
margin-top: 5px;
}
button {
width: 80px;
}
button:hover {
cursor: pointer;
}
</style>
</head>
<body>
<fieldset>
<legend>用户登录</legend>
<div>
<form action="handle.php?action=login" method="post">
<div>
<label for="email">邮箱:</label>
<input type="email" name="email" id="email" placeholder="demo@email.com" required autofocus>
</div>
<div>
<label for="password">密码:</label>
<input type="password" name="password" id="password" placeholder="不少于6位" required>
</div>
<div>
<button>提交</button>
</div>
</form>
<a href="register.php">还没有帐号, 注册一个吧</a>
</div>
</fieldset>
</body>
</html>
register.php 注册页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- <link rel="stylesheet" type="text/css" href="css/style.css"> -->
<title>注册用户</title>
<style>
body {
background: linear-gradient(100deg, white, #00d5ff)
}
fieldset {
width: 300px;
background: linear-gradient(100deg, #00d5ff, #00aaff);
margin: 200px auto;
}
legend {
background-color: white;
margin: auto;
font-size: larger;
}
fieldset>div {
width: 240px;
margin: auto;
padding: 10px;
}
fieldset>div form div {
margin-top: 5px;
}
button {
width: 80px;
}
button:hover {
cursor: pointer;
}
</style>
</head>
<body>
<fieldset>
<legend>用户注册</legend>
<div>
<form action="handle.php?action=register" method="post" onsubmit="return compare()">
<div>
<label for="name">呢称:</label>
<input type="text" name="name" id="name" placeholder="不少于3个字符" required autofocus>
</div>
<div>
<label for="email">邮箱:</label>
<input type="email" name="email" id="email" placeholder="demo@email.com" required>
</div>
<div>
<label for="p1">密码:</label>
<input type="password" name="p1" id="p1" placeholder="不少于6位" required>
</div>
<div>
<label for="p2">重复:</label>
<input type="password" name="p2" id="p2" placeholder="必须与上面一致" required>
</div>
<div>
<button>提交</button><span id="tips" style="color: red"></span>
</div>
</form>
<a href="login.php">我有帐号,直接登录</a>
</div>
</fieldset>
<script>
// 验证二次密码是否相等?
function compare() {
if (document.forms[0].p1.value.trim() !== document.forms[0].p2.value.trim()) {
document.querySelector('#tips').innerText = '二次密码不相等';
return false;
}
}
</script>
</body>
</html>
handle.php 会话处理控制器
<?php
/**
* 会话处理控制器
* 处理登录、注册、退出操作
*/
namespace headerController;
use PDO;
// 开启会话:必须写在最前面
session_start();
// 查询用书表中的数据user表
$db = new PDO('mysql:dbname=phpedu', 'root', 'root');
$stmt = $db->prepare('SELECT * FROM `user`;');
$stmt->execute();
// 得到所有用户数据
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);
//print_r($users);
// 获取用户操作类型 login/register/logout
// strtolower 转小写
$action = strtolower($_GET['action']);
// 根据类型进行不同的操作
switch ($action) {
// 1.登录
case 'login':
// 检查请求方式的类型
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// 获取用户请求的数据:邮箱和密码
$email = $_POST['email'];
// sha1() 加密函数 返回长度为40的字符串
$password = sha1($_POST['password']);
// array_filter 过滤用户数据,在数据表中查找$email和$password,有,返回结果集,没有返回false
$result = array_filter($users, function ($user) use ($email, $password) {
return $user['email'] === $email && $user['password'] === $password;
});
// print_r($result);
// die;
// 判断 $result 是否有数据
if (count($result) > 1) {
// 将用户信息通过session保存serialize序列化后的数据到服务器上
$_SESSION['user'] = serialize(array_slice($result, 0));
exit('<script>alert("验证通过");location.href="index.php"</script>');
} else {
// 登录失败 提示用户未注册
exit("<script>alert('登录失败,邮箱: " . $email . "未注册');location.href='login.php'</script>");
}
} else {
exit('请求类型错误');
}
// 2.退出
case 'logout':
// 判断session数据是否为空
if (isset($_SESSION['user'])) {
// 销毁session 连文件一起删除
session_destroy();
exit('<script>alert("退出成功");location.href="index.php"</script>');
} else {
exit('系统错误, session不存在');
}
// 3.注册
case 'register':
// 获取新用户的数据
$email = $_POST['email'];
$name = $_POST['name'];
$password = sha1($_POST['p2']);
$register_data = time();
// 检查邮箱是否存在
$result = array_filter($users, function ($user) use ($email) {
return $user['email'] === $email;
});
print_r(count($result));
// 判断邮箱是否已注册
if (count($result) > 1) {
exit("<script>alert('注册失败,邮箱: " . $email . "已注册');location.href='register.php'</script>");
} else {
$sql = <<< SQL
INSERT `user`
SET `name`= ?,
`email`= ?,
`password`= ?,
`register_data`= ?
SQL;
// 查询新用户信息
$stmt = $db->prepare($sql);
$data = [$name, $email, $password, $register_data];
if ($stmt->execute($data)) {
if ($stmt->rowCount() > 0) {
// 注册成功之后,让用户自动登录
$sql = 'SELECT * FROM `user` WHERE `id` = ' . $db->lastInsertId();
$stmt = $db->prepare($sql);
$stmt->execute();
$newUser = $stmt->fetchAll(PDO::FETCH_ASSOC);
// 序列化 保存到session
$_SESSION['user'] = serialize($newUser);
exit('<script>alert("注册成功");location.href="index.php"</script>');
} else {
exit('<script>alert("注册失败");location.href="register.php"</script>');
}
} else {
// 输出sql执行错误信息
print_r($stmt->errorInfo());
}
}
default:
// 提示消息后结束执行
exit('参数非法或未定义操作');
}
效果预览