Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:可以看出, 不论是基于cookie, 还是session, 都离不开cookie的支持
代码举例:
<?php
// 判断是否已经登录?
if (isset($_COOKIE['user'])) $user = unserialize($_COOKIE['user']);
?>
<!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/index.css">
<title>首页</title>
</head>
<body>
<nav>
<a href="index.php">首页</a>
<?php if (isset($user)) : ?>
<a href="" id="logout"><span style="color:red"><?php echo $user['name']?></span> 退出</a>
<?php else: ?>
<a href="">音乐</a>
<a href="">舞蹈</a>
<a href="">绘画</a>
<a href="">雕塑</a>
<a href="">书法</a>
<a href="">戏曲</a>
<a href="">相声</a>
<a href="">小品</a>
<a href="">设计</a>
<a href="login.php">登录</a>
<a href="register.php">注册</a>
<?php endif ?>
</nav>
</body>
<script>//(这一段为js代码)
document.querySelector('#logout').addEventListener('click', function(event) {
if (confirm('是否退出')) {
event.preventDefault();
window.location.assign('handle.php?action=logout');
}
});
</script>
</html>
输出效果:
代码举例:
<!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>
</head>
<body>
<h3>用户注册</h3>
<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>
<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>
输出效果:
代码举例:
<?php
// 判断是否已登录
if (isset($_COOKIE['user']))
exit('<script>alert("请不要重复登录");location.href="index.php";</script>');
?>
<!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>
</head>
<body>
<!-- 只需要验证用户的邮箱和密码就可以 -->
<h3>用户登录</h3>
<form action="handle.php?action=login" method="post">
<div>
<label for="email">邮箱:</label>
<input type="email" name="email" id="email" placeholder="demo@email.com" require 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>
</body>
</html>
输出效果:
代码举例:
<?php
// 查询用户表中的数据
$pdo = new PDO('mysql:host=localhost;dbname=phpedu', 'root', 'root');
$stmt = $pdo->prepare('SELECT * FROM `users`');
$stmt->execute();
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);
$action = $_GET['action'];
switch ( strtolower($action)) {
case 'login':
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$email = $_POST['email'];
$password = sha1($_POST['password']);
$results = array_filter($users, function($user) use ($email, $password) {
return $user['email'] === $email && $user['password'] === $password;
});
if (count($results) === 1) {
setcookie('user', serialize(array_pop($results)));
exit('<script>alert("验证通过");location.href="index.php"</script>');
} else {
exit('<script>alert("邮箱或密码错误,或者还没有帐号");location.href="login.php";</script>');
}
} else {
die('请求类型错误');
}
break;
case 'logout':
if (isset($_COOKIE['user'])) {
setcookie('user', null , time()-3600);
exit('<script>alert("退出成功");location.assign("index.php")</script>');
}
break;
case 'register':
$name = $_POST['name'];
$email = $_POST['email'];
$password = sha1($_POST['p1']);
$register_time = time();
$sql = "INSERT `users` SET `name`='{$name}', `email`='{$email}', `password`='{$password}', `register_time`={$register_time}";
$stmt = $pdo->prepare($sql);
$stmt->execute();
if ($stmt->rowCount() === 1) exit('<script>alert("注册成功");location.assign("login.php")</script>');
else exit('<script>alert("注册失败");location.assign("login.php")</script>');
break;
default:
exit('未定义操作');
}
输出效果:
session会话控制与cookie的会话控制类似,但是需要在执行代码前先开启会话“session_start()”,其余代码与cookie类似,不作赘述。
1.PHP session 可以在服务器上存储用户信息以便随后使用。
2.会话信息是临时的,在用户离开网站后将被删除。如果需要永久储存信息,可以把数据存储在数据库中。
3.删除session 数据,可以使用 unset() 或 session_destroy() 函数。
但是session_destroy() 会重置 session,您将失去所有已经存储的 session 数据。