首先是一个普通的登陆页面实现
登录页面login.PHP
<!DOCTYPE HTML><HTML> <head> <Title>登陆页</Title> <link rel="stylesheet" href="https://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" @R_301_6889@="action" value="login"> <div> <label for="user@R_301_6889@">用户名</label> <input type="text" class="form-control" ID="user@R_301_6889@" @R_301_6889@="user@R_301_6889@" placeholder="请输入用户名"> </div> <div> <label for="password">密码</label> <input type="password" class="form-control" ID="password" @R_301_6889@="password" placeholder="请输入密码"> </div> <div class="form-group form-check"> <input type="checkBox" class="form-check-input" ID="remember" @R_301_6889@="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': $user@R_301_6889@ = $_POST['user@R_301_6889@']; $password = $_POST['password']; $remember = $_POST['remember']; $user = getUser(); if ($user@R_301_6889@ != $user['user@R_301_6889@']) { // 登录失败 sendLoginFailedResponse(); } if ($password != $user['password']) { // 登录失败 sendLoginFailedResponse(); } if ($remember) { rememberLogin($user@R_301_6889@); } $_SESSION['user@R_301_6889@'] = $user@R_301_6889@; header("location:index.PHP"); break; case 'logout': session_unset(); setcookie("user@R_301_6889@", "", time() - 1); header("location:login.PHP"); break; } function getUser() { return array( "user@R_301_6889@" => "cyy", "password" => "123456" ); } function sendLoginFailedResponse() { $response = "<script> alert('用户名或密码错误!'); window.location='login.PHP'; </script>"; echo $response; dIE; } function rememberLogin($user@R_301_6889@) { setcookie("user@R_301_6889@", $user@R_301_6889@, time() + 7 * 24 * 3600); }
首页index.PHP
<?PHP session_start(); if (rememberedLogin()) { $_SESSION['user@R_301_6889@'] = $_cookie['user@R_301_6889@']; } if (!hasLoggedIn()) { header("location:login.PHP"); dIE; } function hasLoggedIn() { return isset($_SESSION['user@R_301_6889@']) && valIDateUser@R_301_6889@($_SESSION['user@R_301_6889@']); } function valIDateUser@R_301_6889@($user@R_301_6889@) { return $user@R_301_6889@ == "cyy"; } function rememberedLogin() { return isset($_cookie['user@R_301_6889@']) && valIDateUser@R_301_6889@($_cookie['user@R_301_6889@']); } ?> <!DOCTYPE HTML> <HTML> <head> <Title>主页</Title> <link rel="stylesheet" href="https://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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-Transitional.dtd"> <HTML xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <Meta http-equiv="Content-Type" content="text/HTML;charset=UTF-8"> <Title>许愿墙</Title> <link rel="stylesheet" href="CSS/index.CSS" /> <script type="text/JavaScript" src='Js/jquery-1.7.2.min.Js'></script> <script type="text/JavaScript" src='Js/index.Js'></script> </head> <body> <div ID='top'> <a href="wish.PHP"><span ID='send'></span></a> </div> <div ID='main'> <?PHP //连接数据库 $connection=MysqLi_connect('127.0.0.1','root','123456'); if(MysqLi_connect_error()){ dIE(MysqLi_connect_error()); } MysqLi_select_db($connection,'wall'); MysqLi_set_charset($connection,'utf8'); $sql="SELECT * FROM wall"; $result=MysqLi_query($connection,$sql); //显示留言 while($row=MysqLi_fetch_assoc($result)){ $wish_time=$row['wish_time']; $time=date('Y-m-d H:i:s',$wish_time); $ID=$row['ID']; //判断留言板颜色 switch($row['color']){ case 'a1': echo "<dl class='paper a1'>"; break; case 'a2': echo "<dl class='paper a2'>"; break; case 'a3': echo "<dl class='paper a3'>"; break; case 'a4': echo "<dl class='paper a4'>"; break; case 'a5': echo "<dl class='paper a5'>"; break; default: echo "<dl class='paper a1'>"; break; } echo "<dt>"; echo "<span>{$row['@R_301_6889@']}</span>"; echo "<span>No.{$row['ID']}</span>"; echo "</dt>"; echo "<dd>{$row['content']}</dd>"; echo "<dd>"; echo "<span>{$time}</span>"; echo "<a href=\"delete.PHP?num={$ID}\"></a>"; echo "</dd>"; echo "</dl>"; } MysqLi_close($connection); ?> </div> <!--[if IE 6]> <script type="text/JavaScript" src="./Js/IEpng.Js"></script> <script type="text/JavaScript"> DD_belatedpnG.fix('#send,#close,.close','background'); </script> <![endif]--> </body> </HTML>
添加愿望页面wish.PHP
<!DOCTYPE > <HTML xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <Meta http-equiv="Content-Type" content="text/HTML;charset=UTF-8"> <Title>许愿墙</Title> <link rel="stylesheet" href="CSS/index.CSS" /> <script type="text/JavaScript" src='Js/jquery-1.7.2.min.Js'></script> <script type="text/JavaScript" src='Js/index.Js'></script> <style type="text/CSS"> #content { wIDth: 638px; height:650px; margin:0 auto; margin-top:100px; /*background-color:#F0FAFF; border:2px solID #C9F;*/ } #content .c-top{ wIDth: 638px; height: 80px; background: url(./Images/content_top.jpg) no-repeat; } #content .c-bottom{ wIDth: 638px; height: 50px; background: url(./Images/content_bottom.jpg) no-repeat; } .c-content{ wIDth: 638px; height: 470px; background: url(./Images/content_bg.jpg) repeat; } .papercolor{ wIDth:588px; height: 60px; margin-left: 35px; padding-top:15px; } .p-left{ float: left; wIDth: 120px; line-height: 27px; }p-left .p-right{ float: left; } .color330{ float: left; margin-left: 20px; border-right: #404040 1px solID; border-top: #404040 1px solID; border-left:#404040 1px solID; wIDth: 25px; cursor: pointer; border-bottom: #404040 1px solID; height: 25px; } .papercontent{ wIDth: 588px; height: 210px; margin-left: 35px; } .left{ wIDth: 294px; height:100px; float: left; } .right{ wIDth: 294px; height:100px; float: left; } .left-top{ margin-bottom: 10px; } .left-bottom{ } .right-top{ margin-bottom: 10px; } .right-bottom{ wIDth:200px; height:150px; border: 1px solID orange; margin-left:20px; background-color:#E8DEFF; } .@R_301_6889@{ clear: both; wIDth: 588px; height: 50px; margin-left: 35px; margin-top:10px; } .@R_301_6889@-left{ wIDth:60px; height: 26px; line-height: 26px; float: left; } .@R_301_6889@-right{ float: left; } .@R_301_6889@-right input{ wIDth: 200px; height: 26px; } .code{ clear: both; wIDth: 588px; height: 50px; margin-left: 35px; margin-top:10px; } .code-left{ wIDth:50px; height: 26px; line-height: 26px; float: left; } .code-content{ wIDth:100px; float: left; } .code-content input{ wIDth: 100px; height: 26px; } .code-right{ float:left; margin-left: 10px; } .code-right input{ wIDth: 40px; height: 26px; background-color: pink; } .submit{ wIDth:174px; height:38px; background: url(./Images/pic_submit.gif) no-repeat; margin-left:217px; } .shu@R_301_6889@{ wIDth:80px; height:25px; margin-left: 120px; } span{ Font-size: 13px; Font-family: "微软雅黑"; } </style> </head> <body> <div ID='top'></div> <div ID="content"> <div></div> <form action="add.PHP" method="post" ID="myfrom"> <div> <div> <div> <span>请选择纸条颜色:</span> </div> <div> <div ID="a1" style="background:#FFDFFF"></div> <div ID="a2" style="background:#C3FEC0"></div> <div ID="a3" style="background:#FFE3b8"></div> <div ID="a4" style="background:#CEECFF"></div> <div ID="a5" style="background:#E8DEFF"></div> <input type="hIDden" value="" @R_301_6889@="IDvalue" ID="IDvalue"> </div> </div> <div> <div> <div> <span>输入你的祝福纸条内容:</span> </div> <div> <textarea cols="25" rows="8" ID="textFont" @R_301_6889@="textFont"></textarea> </div> </div> <div> <div> <span>纸条效果预览:</span> </div> <div> <div style="height:15px"><span>第x条</span><br/></div> <div style="height:100px;margin-top:10px"><span ID="Font"></span></div> <div><span ID="@R_301_6889@">署名:</span></div> </div> </div> </div> <div> <div> <span>您的署名:</span> </div> <div> <input ID="@R_301_6889@right" type="text" @R_301_6889@="@R_301_6889@" value=""> </div> </div> <div> <div> <span>验证码:</span> </div> <div> <input ID="codeone" type="text" @R_301_6889@="recode" value=""><span></span> </div> <div> <input ID="codetwo" type="text" @R_301_6889@="code" value="<?PHP echo mt_rand(1000,9999); ?>" Readonly> </div> </div> <!--<div><button type="submit" style="wIDth:174px;height:38px"></button></div>--> <input style="border-RIGHT: #f33b78 1px outset; border-top: #f33b78 1px outset; Font-WEIGHT: bold; border-left: #f33b78 1px outset; color: #ffffff; border-BottOM: #f33b78 1px outset; BACKGROUND-color: #70ae0b;margin-left: 225px" type="submit" value="→ 开始贴许愿小纸条 ←" @R_301_6889@="submit" ID="submit"> <a href="index.PHP"><input type="button" @R_301_6889@="submit2" value="返回"></a> </div> </form> <hr/ style="color:orange;wIDth:550"> <div></div> </div> <!--[if IE 6]> <script type="text/JavaScript" src="./Js/IEpng.Js"></script> <script type="text/JavaScript"> DD_belatedpnG.fix('#send,#close,.close','background'); </script> <![endif]--> <script type="text/JavaScript"> //改变颜色 $(".color330").click(function(){ var value=$(this).CSS("background-color"); var IDvalue=$(this).attr("ID"); console.log(IDvalue); $("#IDvalue").attr("value",IDvalue); $(".right-bottom").CSS("background-color",value); }) //改变值触发的事件 var textFont = document.getElementByID('textFont'); var Font = document.getElementByID('Font'); textFont.onchange=function(){ Font.INNERHTML=textFont.value; } //改变值触发的事件 var @R_301_6889@right = document.getElementByID('@R_301_6889@right'); @[email protected]=function(){ document.getElementByID("@R_301_6889@").innerText="署名: "+@[email protected]; } //在填写完毕验证码之后验证是否一致 var codeone = document.getElementByID('codeone'); var codetwo = document.getElementByID('codetwo'); //表单时区焦点事件 codeone.onblur=function(){ //验证两次验证码是否一致 if(codeone.value != codetwo.value){ this.nextSibling.INNERHTML='验证码不一致!' this.nextSibling.style.color='red'; } } $( '#submit' ).click( function () { window.location.href="add.PHP"; } ); </script> </body> </HTML>
新增愿望实现add.PHP
<?PHP // 获取表单提交数据 $@R_301_6889@=$_POST['@R_301_6889@']; $textFont=$_POST['textFont']; $wish_time=time(); $color=$_POST['IDvalue']; // 数据库 *** 作 $connection=MysqLi_connect('127.0.0.1','root','123456'); if(MysqLi_connect_error()){ dIE(MysqLi_connect_error()); } MysqLi_select_db($connection,'wall'); MysqLi_set_charset($connection,'utf8'); $sql="INSERT INTO wall(content,@R_301_6889@,wish_time,color) VALUES('$textFont','$@R_301_6889@',$wish_time,'$color')"; $result=MysqLi_query($connection,$sql); if($result){ echo '<script>alert("发布成功!");document.location = "index.PHP";</script>'; }else{ echo '<script>alert("发布失败!");document.location = "index.PHP";</script>'; } MysqLi_close($connection); ?>
删除愿望delete.PHP
<?PHP //接受要删除的留言ID $num=$_GET['num']; // 数据库 *** 作 $connection=MysqLi_connect('127.0.0.1','root','123456'); if(MysqLi_connect_error()){ dIE(MysqLi_connect_error()); } MysqLi_select_db($connection,'wall'); MysqLi_set_charset($connection,'utf8'); $sql="DELETE FROM wall WHERE ID=$num"; $result=MysqLi_query($connection,$sql); if($result){ echo '<script>alert("删除成功!");document.location = "index.PHP";</script>'; }else{ echo '<script>alert("删除失败!");document.location = "index.PHP";</script>'; } MysqLi_close($connection); ?>
附上数据库结构wall.sql
-- phpmyadmin sql Dump-- version 4.8.5-- https://www.phpmyadmin.net/---- 主机: localhost-- 生成日期: 2019-08-18 22:08:38-- 服务器版本: 8.0.12-- PHP 版本: 7.3.4SET 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 @R_301_6889@S 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 '留言内容', `@R_301_6889@` 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`, `@R_301_6889@`, `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 */;
知识点补充:
【使用 cookie 实现会话控制】
用于存储用户关键信息
保存在客户端(浏览器)
通过 http 请求/响应头传输
【cookie 失效】
● cookie过期
● 用户手动删除 cookie
● 服务器清除 cookie 的有效性
【使用 SESSION 实现会话控制】
● 用于存储用户相关信息
● 保存在服务端
● 通过保存在客户端的 SESSION ID 来定位 SESSION 内容
【SESSION 失效/清除】
● cookie过期(关闭浏览器)
● 用户手动删除 cookie
● 服务端删除 SESSION 文件或清空 SESSION 内容
更多相关PHP知识,请访问php教程! 总结
以上是内存溢出为你收集整理的利用会话控制实现页面登录与注销功能全部内容,希望文章能够帮你解决利用会话控制实现页面登录与注销功能所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)