
批改狀態(tài):合格
老師批語:
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>
// 為退出按鈕創(chuàng)建事件監(jiān)聽器
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();
// 查詢用書表中的數(shù)據user表
$db = new PDO('mysql:dbname=phpedu', 'root', 'root');
$stmt = $db->prepare('SELECT * FROM `user`;');
$stmt->execute();
// 得到所有用戶數(shù)據
$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') {
// 獲取用戶請求的數(shù)據:郵箱和密碼
$email = $_POST['email'];
// sha1() 加密函數(shù) 返回長度為40的字符串
$password = sha1($_POST['password']);
// array_filter 過濾用戶數(shù)據,在數(shù)據表中查找$email和$password,有,返回結果集,沒有返回false
$result = array_filter($users, function ($user) use ($email, $password) {
return $user['email'] === $email && $user['password'] === $password;
});
// print_r($result);
// die;
// 判斷 $result 是否有數(shù)據
if (count($result) > 1) {
// 將用戶信息通過session保存serialize序列化后的數(shù)據到服務器上
$_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數(shù)據是否為空
if (isset($_SESSION['user'])) {
// 銷毀session 連文件一起刪除
session_destroy();
exit('<script>alert("退出成功");location.href="index.php"</script>');
} else {
exit('系統(tǒng)錯誤, session不存在');
}
// 3.注冊
case 'register':
// 獲取新用戶的數(shù)據
$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執(zhí)行錯誤信息
print_r($stmt->errorInfo());
}
}
default:
// 提示消息后結束執(zhí)行
exit('參數(shù)非法或未定義操作');
}
效果預覽
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號