批改状态:合格
老师批语:这个案例实际上就是一个小型的MVC, 当然并没有提及它
写一个完整的登录验证功能,要求用cookie和session分别实现
一、cookie
1、入口文件:index.php
<?php
// 为了简化程序,使用了一个中间层:请求派发器,类似于框架的控制器,对用户的请求进行集中处理
// 1、已登陆:显示出用户的登陆信息,显示退出按钮
if(isset($_COOKIE['username']) && $_COOKIE['username'] ==='admin' ){
echo '欢迎:' . $_COOKIE['username'] . '<br/>';
echo '<a href="dispatch.php?action=logout">退出</a>';
}else{
// 2、未登陆,就跳转到登陆页面
echo '<a href="dispatch.php?action=login">请登陆</a>';
}
2、请求派发器:前端控制器 dispatch.php
<?php
// 请求派发器:前端控制器
// 功能就是获取到用户的请求,并调用不同的脚本进行处理和响应
// 连接数据库
require __DIR__ . '/inc/connect.php';
// 获取请求参数
$action = isset($_GET['action']) ? $_GET['action'] : 'login';
// trim() 函数移除字符串两侧的空白字符或其他预定义字符
// strtolower()函数把字符串转换为小写,strtoupper()把字符串转换为大写
// htmlentities()函数把字符转换为 HTML 实体 / html_entity_decode()函数把HTML实体转换回字符
$action = htmlentities(strtolower(trim($action)));
// 请求分发
switch ($action){
// 1、登陆
case 'login':
include __DIR__ . '/login.php';
break;
// 2、验证登陆
case 'check':
include __DIR__ . '/check.php';
break;
// 3、退出登陆
case 'logout':
include __DIR__ . '/logout.php';
break;
// 4、默认操作:
//header('Locatioin:index.php');
echo '<script>location.assign("index.php");</script>';
}3、登陆:login.php
<?php
// 防止用户重复登陆
if(isset($_COOKIE['username']) && $_COOKIE['username'] === 'admin' ){
echo '<script>alert("不能重复登陆");location.assign("index.php");</script>';
}
?>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>用户登陆</title>
</head>
<body>
<h3>用户登陆</h3>
<form action="dispatch.php?action=check" method="post" onsubmit="return isEmpty();">
<p><label for="email">邮箱:</label>
<input type="email" name="email" id="email"></p>
<p><label for="password">密码:</label>
<input type="password" name="password" id="password"></p>
<p><button>提交</button></p>
</form>
<script>
function isEmpty() {
var email= document.getElementById('email').value;
var password = document.getElementById('password').value;
if(email.length === 0 || password.length === 0){
alert("邮箱和密码不能为空!");
return false;
}
}
</script>
</body>
</html>
4、验证页面:check.php
<?php
// 1、判断用户的请求类型是否正确
if($_SERVER['REQUEST_METHOD'] === 'POST'){
// 2、获取表单数据
$email = $_POST['email'];
$password = sha1($_POST['password']);
// 3、用户信息验证
$sql = 'SELECT * FROM `user` WHERE `email` = :email AND `password` = :password LIMIT 1';
$stmt =$pdo->prepare($sql);
$stmt->execute(['email'=>$email, 'password'=>$password]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
// 4、判断验证的结果
if( false === $user){
// 验证失败,返回上一页面
echo '<script>alert("验证失败");history.back();</script>';
die;
}
// 验证成功
// 将用户的信息写到cookie中
setcookie('username', $user['username']);
echo '<script>alert("登陆成功!");location.assign("index.php");</script>';
}else{
die('请求类型错误!');
}验证通过,跳转到首页

5、退出页面:logout.php
<?php
// 必须在用户已经登陆的情况下再退出
if(isset($_COOKIE['username']) && $_COOKIE['username'] === 'admin'){
setcookie('username', null, time()-3600);
echo '<script>alert("退出成功!");location.assign("index.php");</script>';
}else{
// 要求用户先登陆
echo '<script>alert("请先登陆!");location.assign("login.php")</script>';
}退出后跳转到首页

二、session:
1、index.hp
<?php
session_start();
// 为了简化程序,使用了一个中间层:请求派发器,类似于框架的控制器,对用户的请求进行集中处理
// 1、已登陆:显示出用户的登陆信息,显示退出按钮
if(isset($_SESSION['username']) && $_SESSION['username'] ==='admin' ){
echo '欢迎:' . $_SESSION['username'] . '<br/>';
echo '<a href="dispatch.php?action=logout">退出</a>';
}else{
// 2、未登陆,就跳转到登陆页面
echo '<a href="dispatch.php?action=login">请登陆</a>';
}2、请求派发器:前端控制器 dispatch.php
<?php
// 请求派发器:前端控制器
// 功能就是获取到用户的请求,并调用不同的脚本进行处理和响应
// 连接数据库
require __DIR__ . '/inc/connect.php';
// 获取请求参数
$action = isset($_GET['action']) ? $_GET['action'] : 'login';
// trim() 函数移除字符串两侧的空白字符或其他预定义字符
// strtolower()函数把字符串转换为小写,strtoupper()把字符串转换为大写
// htmlentities()函数把字符转换为 HTML 实体 / html_entity_decode()函数把HTML实体转换回字符
$action = htmlentities(strtolower(trim($action)));
// 请求分发
switch ($action){
// 1、登陆
case 'login':
include __DIR__ . '/login.php';
break;
// 2、验证登陆
case 'check':
include __DIR__ . '/check.php';
break;
// 3、退出登陆
case 'logout':
include __DIR__ . '/logout.php';
break;
// 4、默认操作:
//header('Locatioin:index.php');
echo '<script>location.assign("index.php");</script>';
}3、login.php
<?php
// 防止用户重复登陆
if(isset($_SESSION['username']) && $_SESSION['username'] === 'admin' ){
echo '<script>alert("不能重复登陆");location.assign("index.php");</script>';
}
?>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>用户登陆</title>
</head>
<body>
<h3>用户登陆</h3>
<form action="dispatch.php?action=check" method="post" onsubmit="return isEmpty();">
<p><label for="email">邮箱:</label>
<input type="email" name="email" id="email"></p>
<p><label for="password">密码:</label>
<input type="password" name="password" id="password"></p>
<p><button>提交</button></p>
</form>
<script>
function isEmpty() {
var email= document.getElementById('email').value;
var password = document.getElementById('password').value;
if(email.length === 0 || password.length === 0){
alert("邮箱和密码不能为空!");
return false;
}
}
</script>
</body>
</html>4、验证:check.php
<?php
// session_start()函数必须位于<html>标签之前, 启动会话
session_start();
// 1、判断用户的请求类型是否正确
if($_SERVER['REQUEST_METHOD'] === 'POST'){
// 2、获取表单数据
$email = $_POST['email'];
$password = sha1($_POST['password']);
// 3、用户信息验证
$sql = 'SELECT * FROM `user` WHERE `email` = :email AND `password` = :password LIMIT 1';
$stmt =$pdo->prepare($sql);
$stmt->execute(['email'=>$email, 'password'=>$password]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
// 4、判断验证的结果
if( false === $user){
// 验证失败,返回上一页面
echo '<script>alert("验证失败");history.back();</script>';
die;
}
// 验证成功
// 将用户的信息写到cookie中
// setcookie('username', $user['username']);
// 将用户登录信息写到session中
$_SESSION['username'] = $user['username'];
echo '<script>alert("登陆成功!");location.assign("index.php");</script>';
}else{
die('请求类型错误!');
}5、退出:logout.php
<?php
session_start();
// 必须在用户已经登陆的情况下再退出
if(isset($_SESSION['username']) && $_SESSION['username'] === 'admin'){
//setcookie('username', null, time()-3600);
// 销毁 session
// session_destroy() 将重置 session,您将失去所有已存储的 session 数据。
// unset() 函数用于释放指定的 session 变量
session_unset();
//session_destroy();
setcookie('PHPSESSID', null, time()-3600);
echo $_COOKIE['PHPSESSID'];
echo '<script>alert("退出成功!");location.assign("index.php");</script>';
}else{
// 要求用户先登陆
echo '<script>alert("请先登陆!");location.assign("login.php")</script>';
}
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号