如何通过会话控制技术实现网页登录与注销功能?

2026-04-06 18:361阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1710个文字,预计阅读时间需要7分钟。

如何通过会话控制技术实现网页登录与注销功能?

首先是一个常见的登录页面实现,包括登录页面和登录表单。以下是简化后的代码:

登录页面

如何通过会话控制技术实现网页登录与注销功能?

首先是一个普通的登陆页面实现

登录页面login.php

<!DOCTYPE html> <html> <head> <title>登陆页</title> <link rel="stylesheet" href="stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous"> </head> <body> <div> <div class="card col-12 mt-5"> <div> <h4> 用户登录 </h4> <div class="col-12 mt-4 d-flex justify-content-center"> <form method="post" action="action.php"> <input type="hidden" name="action" value="login"> <div> <label for="username">用户名</label> <input type="text" class="form-control" id="username" name="username" placeholder="请输入用户名"> </div> <div> <label for="password">密码</label> <input type="password" class="form-control" id="password" name="password" placeholder="请输入密码"> </div> <div class="form-group form-check"> <input type="checkbox" class="form-check-input" id="remember" name="remember"> <label for="remember"> 在这台电脑上记住我的登录状态 </label> </div> <button type="submit" class="btn btn-primary"> 登录 </button> </form> </div> </div> </div> </div> </body> </html>

登录功能实现action.php

<?php session_start(); switch ($_REQUEST['action']) { case 'login': $username = $_POST['username']; $password = $_POST['password']; $remember = $_POST['remember']; $user = getUser(); if ($username != $user['username']) { // 登录失败 sendLoginFailedResponse(); } if ($password != $user['password']) { // 登录失败 sendLoginFailedResponse(); } if ($remember) { rememberLogin($username); } $_SESSION['username'] = $username; header("location:index.php"); break; case 'logout': session_unset(); setcookie("username", "", time() - 1); header("location:login.php"); break; } function getUser() { return array( "username" => "cyy", "password" => "123456" ); } function sendLoginFailedResponse() { $response = "<script> alert('用户名或密码错误!'); window.location='login.php'; </script>"; echo $response; die; } function rememberLogin($username) { setcookie("username", $username, time() + 7 * 24 * 3600); }

首页index.php

<?php session_start(); if (rememberedLogin()) { $_SESSION['username'] = $_COOKIE['username']; } if (!hasLoggedIn()) { header("location:login.php"); die; } function hasLoggedIn() { return isset($_SESSION['username']) && validateUsername($_SESSION['username']); } function validateUsername($username) { return $username == "cyy"; } function rememberedLogin() { return isset($_COOKIE['username']) && validateUsername($_COOKIE['username']); } ?> <!DOCTYPE html> <html> <head> <title>主页</title> <link rel="stylesheet" href="stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous"> </head> <body> <div> <nav class="navbar navbar-light bg-light"> <a> 使用 Cookie 和 Session 实现会话控制 </a> <a href="action.php?action=logout"> <button class="btn btn-outline-danger my-2 my-sm-0" type="button"> 注销 </button> </a> </nav> <div class="d-flex justify-content-around mt-5"> <div class="card col-5"> <div> <h5> 会话控制实战内容一 </h5> <h6 class="card-subtitle mb-2 text-muted"> SESSION 部分 </h6> <p> 实现用户认证功能,用户登录、退出与身份识别 </p> </div> </div> <div class="card col-5"> <div> <h5> 会话控制实战内容二 </h5> <h6 class="card-subtitle mb-2 text-muted"> COOKIE 部分 </h6> <p> 实现登录记住用户功能,七天免登录认证 </p> </div> </div> </div> <div class="d-flex justify-content-around mt-4"> <div class="card col-5"> <div> <h5> 会话控制实战内容一 </h5> <h6 class="card-subtitle mb-2 text-muted"> SESSION 部分 </h6> <p> 实现用户认证功能,用户登录、退出与身份识别 </p> </div> </div> <div class="card col-5"> <div> <h5> 会话控制实战内容二 </h5> <h6 class="card-subtitle mb-2 text-muted"> COOKIE 部分 </h6> <p> 实现登录记住用户功能,七天免登录认证 </p> </div> </div> </div> </div> </body> </html>

接下来是会话控制实例:许愿墙源码

许愿墙首页index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta www.phpmyadmin.net/ -- -- 主机: localhost -- 生成日期: 2019-08-18 22:08:38 -- 服务器版本: 8.0.12 -- PHP 版本: 7.3.4 SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; SET AUTOCOMMIT = 0; START TRANSACTION; SET time_zone = "+00:00"; /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8mb4 */; -- -- 数据库: `wall` -- -- -------------------------------------------------------- -- -- 表的结构 `wall` -- CREATE TABLE `wall` ( `id` tinyint(4) NOT NULL COMMENT '留言编号', `content` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '留言内容', `name` varchar(20) NOT NULL DEFAULT '匿名的宝宝' COMMENT '署名', `wish_time` int(11) NOT NULL COMMENT '留言时间', `color` char(2) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '留言背景色' ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- -- 转存表中的数据 `wall` -- INSERT INTO `wall` (`id`, `content`, `name`, `wish_time`, `color`) VALUES (17, '111', '111', 1566136880, 'a1'), (19, '333', '333', 1566136894, 'a3'), (21, '555', '555', 1566136911, 'a5'), (24, '9999', '9999', 1566137235, 'a4'); -- -- 转储表的索引 -- -- -- 表的索引 `wall` -- ALTER TABLE `wall` ADD PRIMARY KEY (`id`); -- -- 在导出的表使用AUTO_INCREMENT -- -- -- 使用表AUTO_INCREMENT `wall` -- ALTER TABLE `wall` MODIFY `id` tinyint(4) NOT NULL AUTO_INCREMENT COMMENT '留言编号', AUTO_INCREMENT=26; COMMIT; /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

知识点补充:

用于存储用户关键信息

保存在客户端(浏览器)

通过 HTTP 请求/响应头传输

● COOKIE过期

● 用户手动删除 COOKIE

● 服务器清除 COOKIE 的有效性

● 用于存储用户相关信息

● 保存在服务端

● 通过保存在客户端的 SESSION ID 来定位 SESSION 内容

● COOKIE过期(关闭浏览器)

● 用户手动删除 COOKIE

● 服务端删除 SESSION 文件或清空 SESSION 内容

更多相关php知识,请访问php教程!

以上就是利用会话控制实现页面登录与注销功能的详细内容,更多请关注自由互联其它相关文章!

本文共计1710个文字,预计阅读时间需要7分钟。

如何通过会话控制技术实现网页登录与注销功能?

首先是一个常见的登录页面实现,包括登录页面和登录表单。以下是简化后的代码:

登录页面

如何通过会话控制技术实现网页登录与注销功能?

首先是一个普通的登陆页面实现

登录页面login.php

<!DOCTYPE html> <html> <head> <title>登陆页</title> <link rel="stylesheet" href="stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous"> </head> <body> <div> <div class="card col-12 mt-5"> <div> <h4> 用户登录 </h4> <div class="col-12 mt-4 d-flex justify-content-center"> <form method="post" action="action.php"> <input type="hidden" name="action" value="login"> <div> <label for="username">用户名</label> <input type="text" class="form-control" id="username" name="username" placeholder="请输入用户名"> </div> <div> <label for="password">密码</label> <input type="password" class="form-control" id="password" name="password" placeholder="请输入密码"> </div> <div class="form-group form-check"> <input type="checkbox" class="form-check-input" id="remember" name="remember"> <label for="remember"> 在这台电脑上记住我的登录状态 </label> </div> <button type="submit" class="btn btn-primary"> 登录 </button> </form> </div> </div> </div> </div> </body> </html>

登录功能实现action.php

<?php session_start(); switch ($_REQUEST['action']) { case 'login': $username = $_POST['username']; $password = $_POST['password']; $remember = $_POST['remember']; $user = getUser(); if ($username != $user['username']) { // 登录失败 sendLoginFailedResponse(); } if ($password != $user['password']) { // 登录失败 sendLoginFailedResponse(); } if ($remember) { rememberLogin($username); } $_SESSION['username'] = $username; header("location:index.php"); break; case 'logout': session_unset(); setcookie("username", "", time() - 1); header("location:login.php"); break; } function getUser() { return array( "username" => "cyy", "password" => "123456" ); } function sendLoginFailedResponse() { $response = "<script> alert('用户名或密码错误!'); window.location='login.php'; </script>"; echo $response; die; } function rememberLogin($username) { setcookie("username", $username, time() + 7 * 24 * 3600); }

首页index.php

<?php session_start(); if (rememberedLogin()) { $_SESSION['username'] = $_COOKIE['username']; } if (!hasLoggedIn()) { header("location:login.php"); die; } function hasLoggedIn() { return isset($_SESSION['username']) && validateUsername($_SESSION['username']); } function validateUsername($username) { return $username == "cyy"; } function rememberedLogin() { return isset($_COOKIE['username']) && validateUsername($_COOKIE['username']); } ?> <!DOCTYPE html> <html> <head> <title>主页</title> <link rel="stylesheet" href="stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous"> </head> <body> <div> <nav class="navbar navbar-light bg-light"> <a> 使用 Cookie 和 Session 实现会话控制 </a> <a href="action.php?action=logout"> <button class="btn btn-outline-danger my-2 my-sm-0" type="button"> 注销 </button> </a> </nav> <div class="d-flex justify-content-around mt-5"> <div class="card col-5"> <div> <h5> 会话控制实战内容一 </h5> <h6 class="card-subtitle mb-2 text-muted"> SESSION 部分 </h6> <p> 实现用户认证功能,用户登录、退出与身份识别 </p> </div> </div> <div class="card col-5"> <div> <h5> 会话控制实战内容二 </h5> <h6 class="card-subtitle mb-2 text-muted"> COOKIE 部分 </h6> <p> 实现登录记住用户功能,七天免登录认证 </p> </div> </div> </div> <div class="d-flex justify-content-around mt-4"> <div class="card col-5"> <div> <h5> 会话控制实战内容一 </h5> <h6 class="card-subtitle mb-2 text-muted"> SESSION 部分 </h6> <p> 实现用户认证功能,用户登录、退出与身份识别 </p> </div> </div> <div class="card col-5"> <div> <h5> 会话控制实战内容二 </h5> <h6 class="card-subtitle mb-2 text-muted"> COOKIE 部分 </h6> <p> 实现登录记住用户功能,七天免登录认证 </p> </div> </div> </div> </div> </body> </html>

接下来是会话控制实例:许愿墙源码

许愿墙首页index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta www.phpmyadmin.net/ -- -- 主机: localhost -- 生成日期: 2019-08-18 22:08:38 -- 服务器版本: 8.0.12 -- PHP 版本: 7.3.4 SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; SET AUTOCOMMIT = 0; START TRANSACTION; SET time_zone = "+00:00"; /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8mb4 */; -- -- 数据库: `wall` -- -- -------------------------------------------------------- -- -- 表的结构 `wall` -- CREATE TABLE `wall` ( `id` tinyint(4) NOT NULL COMMENT '留言编号', `content` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '留言内容', `name` varchar(20) NOT NULL DEFAULT '匿名的宝宝' COMMENT '署名', `wish_time` int(11) NOT NULL COMMENT '留言时间', `color` char(2) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '留言背景色' ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- -- 转存表中的数据 `wall` -- INSERT INTO `wall` (`id`, `content`, `name`, `wish_time`, `color`) VALUES (17, '111', '111', 1566136880, 'a1'), (19, '333', '333', 1566136894, 'a3'), (21, '555', '555', 1566136911, 'a5'), (24, '9999', '9999', 1566137235, 'a4'); -- -- 转储表的索引 -- -- -- 表的索引 `wall` -- ALTER TABLE `wall` ADD PRIMARY KEY (`id`); -- -- 在导出的表使用AUTO_INCREMENT -- -- -- 使用表AUTO_INCREMENT `wall` -- ALTER TABLE `wall` MODIFY `id` tinyint(4) NOT NULL AUTO_INCREMENT COMMENT '留言编号', AUTO_INCREMENT=26; COMMIT; /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

知识点补充:

用于存储用户关键信息

保存在客户端(浏览器)

通过 HTTP 请求/响应头传输

● COOKIE过期

● 用户手动删除 COOKIE

● 服务器清除 COOKIE 的有效性

● 用于存储用户相关信息

● 保存在服务端

● 通过保存在客户端的 SESSION ID 来定位 SESSION 内容

● COOKIE过期(关闭浏览器)

● 用户手动删除 COOKIE

● 服务端删除 SESSION 文件或清空 SESSION 内容

更多相关php知识,请访问php教程!

以上就是利用会话控制实现页面登录与注销功能的详细内容,更多请关注自由互联其它相关文章!