Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:
该链接为这个实战的前置代码, https://www.php.cn/blog/detail/26509.html
<?php
session_start();
$handle = $_GET['handle'] ?? $_POST['handle'] ?? 'select';
//var_dump($_POST);
//echo $handle;
//die();
switch ($handle) {
case 'select':
require __DIR__ . '/select.php';
break;
case 'update':
require __DIR__ . '/update.php';
break;
case 'delete':
require __DIR__ . '/delete.php';
break;
case 'login':
require dirname(__DIR__) . '../config/connect.php';
$pdo = connect();
$sql = "SELECT email FROM `users` WHERE email=? AND password=md5(?);";
$stmt = $pdo->prepare($sql);
if ($stmt->execute([$_POST['email'], $_POST['password']])) {
$data = $stmt->fetch();
if (is_array($data) && count($data) === 1) {
// 登录成功, 写入session
$_SESSION['email'] = $_POST['email'];
// 返回响应
echo json_encode(['code' => 0, 'message' => '登录成功, 正在刷新页面....']);
} else {
echo json_encode(['code' => -20005, 'message' => '登录失败, 数据库中没有该用户,请注册!']);
}
} else {
echo json_encode(['code' => $stmt->errorInfo()[0], 'message' => '未知错误, 请联系管理员. : ' . $stmt->errorInfo()[1]]);
}
break;
case 'register':
require dirname(__DIR__) . '../config/connect.php';
$pdo = connect();
$stmt = $pdo->prepare("SELECT email From `users`");
$stmt->bindColumn('email', $email);
$stmt->execute();
$emailAyy = [];
while ($stmt->fetch(PDO::FETCH_BOUND)) {
$emailAyy[] = $email;
}
if (!in_array($_POST['email'], $emailAyy)) {
$sql = "INSERT `users` SET username=?, email=?,password=md5(?)";
$stmt = $pdo->prepare($sql);
$stmt->execute([$_POST['username'], $_POST['email'], $_POST['password']]);
if ($stmt->rowCount() === 1) {
// 注册成功,自动登录
$_SESSION['email'] = $_POST['email'];
echo json_encode(['code' => 0, 'message' => '注册成功,正在跳转....']);
} else {
echo json_encode(['code' => 20004, 'message' => '未知错误,请联系管理员!']);
}
} else {
echo json_encode(['code' => -20003, 'message' => '注册失败, 该邮箱已经注册过了!']);
}
break;
case 'logout':
// 清空session
session_unset();
// 删除session文件
session_destroy();
// 退出登录成功
echo json_encode(['code' => 0, 'message' => '退成登录成功!']);
break;
default:
exit('未知错误');
}
<?php session_start(); ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>信息管理</title>
<link rel="stylesheet" href="./pure-css3-icons/icono.min.css">
<link rel="stylesheet" href="./style.css">
<link rel="stylesheet" href="./user-style.css">
</head>
<body>
<header>
<a href="#">首页</a>
<nav class="user-operation">
<?php if (!isset($_SESSION['email'])): ?>
<a data-handle="login">登录</a>
<a data-handle="register">注册</a>
<?php else: ?>
<span><?= $_SESSION['email'] ?></span>
<a data-handle="logout">退出登录</a>
<?php endif; ?>
</nav>
</header>
<table>
<caption>信息管理</caption>
<thead>
<tr>
<th>序号</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>生日</th>
<th>工资</th>
<th>添加时间</th>
<th>修改时间</th>
<?php if (!empty($_SESSION['email'])) : ?>
<th>操作</th>
<?php endif; ?>
</tr>
</thead>
<tbody class="tbody"></tbody>
</table>
<div class="paging"></div>
<div class="masking">
<div class="del-confirm">
<header>
<h3>警告</h3>
<button onclick="delMaskingClose()"><i class="icono-cross"></i></button>
</header>
<masking-body>
确定删除
</masking-body>
<footer>
<button onclick="delMaskingClose()">取消</button>
<button onclick="delMaskingConfirm()">确定</button>
</footer>
</div>
<div class="edit-confirm">
<header>
<h3>编辑</h3>
<button onclick="editMaskingClose()"><i class="icono-cross"></i></button>
</header>
<masking-body class="edit-form">
</masking-body>
<footer>
<button onclick="editMaskingClose()">取消</button>
<button onclick="editMaskingConfirm()">确定</button>
</footer>
</div>
</div>
<div class="user-masking">
<div class="user-login" style="display: block">
<header>
<h3>登录</h3>
<button onclick="userMaskingClose()"><i class="icono-cross"></i></button>
</header>
<masking-body>
<form id="login">
<label for="email">邮箱: </label>
<input type="email" name="email" id="email" required>
<label for="password">密码: </label>
<input type="password" name="password" id="password">
<button type="button" class="user-submit" data-handle="login">登录</button>
<span class="tip"></span>
<a class="no-account">没有账号? 点击注册</a>
</form>
</masking-body>
</div>
<div class="user-register">
<header>
<h3>注册</h3>
<button onclick="userMaskingClose()"><i class="icono-cross"></i></button>
</header>
<masking-body>
<form id="register">
<label for="username">用户名: </label>
<input type="text" name="username" id="username">
<label for="email">邮箱: </label>
<input type="email" name="email" id="email">
<label for="password">密码: </label>
<input type="password" name="password" id="password">
<label for="password1">确认密码: </label>
<input type="password" name="password1" id="password1">
<button type="button" class="user-submit" data-handle="register">注册</button>
<span class="tip"></span>
<a class="account">已有账号? 点击登录</a>
</form>
</masking-body>
</div>
</div>
<script src="./event.js"></script>
<script src="./user-operation.js"></script>
</body>
</html>
/*
Navicat MySQL Data Transfer
Source Server : php_test
Source Server Version : 50726
Source Host : localhost:3306
Source Database : phpedu
Target Server Type : MYSQL
Target Server Version : 50726
File Encoding : 65001
Date: 2021-03-02 17:04:52
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for users
-- ----------------------------
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
`uid` int(10) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '用户名',
`email` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '邮箱',
`password` varchar(150) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '密码',
`create_d` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
PRIMARY KEY (`uid`),
UNIQUE KEY `unique_email` (`email`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- ----------------------------
-- Records of users
-- ----------------------------
INSERT INTO `users` VALUES ('1', 'admin', 'admin@qq.com', 'e10adc3949ba59abbe56e057f20f883e', '2021-03-02 16:28:15');
INSERT INTO `users` VALUES ('2', 'zhangsan', 'zhangsan@qq.com', 'e10adc3949ba59abbe56e057f20f883e', '2021-03-02 16:43:23');
const form = document.forms;
const xhr = new XMLHttpRequest();
const loginEmail = document.querySelector('#login #email');
const loginPassword = document.querySelector('#login #password');
const registerUsername = document.querySelector('#register #username');
const registerEmail = document.querySelector('#register #email');
const registerPassword = document.querySelector('#register #password');
const registerPassword1 = document.querySelector('#register #password1');
const loginTip = document.querySelector('#login .tip');
const registerTip = document.querySelector('#register .tip');
xhr.responseType = 'json';
[loginEmail, loginPassword, registerUsername, registerEmail, registerPassword, registerPassword1]
.forEach(item => item.oninput = () => [loginTip, registerTip].forEach(it => it.innerHTML = null));
document.querySelector('.user-operation').addEventListener('click', function (ev) {
ev.preventDefault();
console.log(ev.target.getAttribute('data-handle'));
switch (ev.target.getAttribute('data-handle')) {
case 'login':
document.querySelector('.user-masking').style.display = 'block';
document.querySelector('.user-login').style.display = 'block';
break;
case 'register':
document.querySelector('.user-masking').style.display = 'block';
document.querySelector('.user-register').style.display = 'block';
break;
case 'logout':
xhr.open('post', './Controller/handle.php?handle=logout');
xhr.onload = () => {
console.log(xhr.response);
if (xhr.response.code === 0) {
alert(xhr.response.message);
location.href = 'index.php';
}
};
xhr.onerror = (error) => console.log(error);
xhr.send();
break;
default:
alert('未知错误');
}
});
document.querySelectorAll('.user-submit').forEach(item => item.addEventListener('click', function (ev) {
console.log(ev.target.getAttribute('data-handle'));
switch (ev.target.getAttribute('data-handle')) {
case 'login':
login();
break;
case 'register':
register();
break;
default:
alert('未知错误');
}
}));
/**
* 登录方法
*/
function login() {
const login = form.login;
if (login.email.value.trim().length > 0) { // 判断邮箱是否有值
// 判断邮箱格式是否正确
if (/^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/.test(login.email.value.trim())) {
if (login.password.value.trim().length > 0) { // 判断密码是否有值
const formData = new FormData(login);
formData.append('handle', 'login');
xhr.open('post', './Controller/handle.php');
xhr.onload = () => {
console.log(xhr.response);
loginTip.innerHTML = xhr.response.message;
if (xhr.response.code === 0) {
setTimeout(() => location.href = 'index.php', 2000);
}
};
xhr.onerror = (error) => console.log(error);
xhr.send(formData);
} else {
loginTip.innerHTML = '请输入密码!';
login.password.focus();
}
} else {
loginTip.innerHTML = '请输入正确格式的邮箱!';
login.email.focus();
}
} else {
loginTip.innerHTML = '请输入邮箱!';
login.email.focus();
}
}
/**
* 注册方法
*/
function register() {
const register = form.register;
console.log(register);
if (register.username.value.trim().length > 0) { // 判断用户名是否有值
if (register.email.value.trim().length > 0) { // 判断邮箱是否有值
// 判断邮箱格式是否正确
if (/^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/.test(register.email.value.trim())) {
if (register.password.value.trim().length > 0) {// 判断密码是否有值
if (register.password.value.trim() === register.password1.value.trim()) { // 判断两次密码是否相同
const formData = new FormData(register);
formData.delete('password1');
formData.append('handle', 'register');
xhr.open('post', './Controller/handle.php');
xhr.onload = () => {
console.log(xhr.response);
registerTip.innerHTML = xhr.response.message;
if (xhr.response.code === 0) {
setTimeout(() => location.href = 'index.php', 2000);
}
};
xhr.onerror = (error) => console.log(error);
xhr.send(formData);
} else {
registerTip.innerHTML = '请保证二次输入的密码和依次输入的密码相同!';
register.password.focus();
}
} else {
registerTip.innerHTML = '请输入密码!';
register.password.focus();
}
} else {
registerTip.innerHTML = '请输入正确格式的邮箱!';
register.email.focus();
}
} else {
registerTip.innerHTML = '请输入邮箱!';
register.email.focus();
}
} else {
registerTip.innerHTML = '请输入用户名';
register.username.focus();
}
}
document.querySelector('.no-account').addEventListener('click', function (ev) {
document.querySelector('.user-login').style.display = 'none';
document.querySelector('.user-register').style.display = 'block';
});
document.querySelector('.account').addEventListener('click', function (ev) {
document.querySelector('.user-login').style.display = 'block';
document.querySelector('.user-register').style.display = 'none';
});
function userMaskingClose() {
document.querySelector('.user-masking').style.display = 'none';
document.querySelector('.user-login').style.display = 'none';
document.querySelector('.user-register').style.display = 'none';
}
body > header {
display: flex;
align-self: stretch;
padding: 0.5em 1em;
background-color: #333333;
}
body > header > a {
color: #bbbbbb;
}
body > header > nav {
margin-left: auto;
}
body > header > nav a {
color: #bbbbbb;
}
body > header a:hover {
color: #ffffff;
cursor: pointer;
}
.user-masking {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
background-color: rgba(0, 0, 0, .5);
display: none;
}
.user-masking .user-login,
.user-masking .user-register {
width: 446px;
height: 272px;
background-color: #f4eded;
border-radius: 1em;
position: absolute;
margin: auto;
top: 0;
right: 0;
bottom: 0;
left: 0;
display: none;
}
.user-masking .user-register{
height: 342px;
}
.user-masking header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 8px 16px;
border-bottom: 1px solid #3a3a3a;
}
.user-masking header button {
background-color: transparent;
border: none;
height: 34px;
width: 50px;
cursor: pointer;
}
.user-masking masking-body {
height: calc(100% - 50px);
display: block;
padding: 16px;
}
.user-masking masking-body form > label {
/*place-self: center;*/
text-align: left;
line-height: 2em;
}
.user-masking masking-body form > input{
padding: 0 .5em;
line-height: 2em;
}
.user-masking masking-body form {
display: grid;
gap: .5em 0;
width: 100%;
padding: 1em 1em;
grid-template-columns: 4.5em 1fr;
}
.user-submit{
grid-area: span 1 / span 2;
place-self: center;
border: none;
border-radius: .25em;
background-color: #3485FB;
padding: .5em 1em;
}
.tip{
grid-area: span 1 / span 2;
place-self: center;
}
.no-account,
.account{
grid-area: span 1 / span 2;
place-self: center;
}
.no-account:hover,
.account:hover{
color: #7f007f;
cursor: pointer;
}