7月26日作业:
写一个完整的登录验证功能,要求用cookie和session分别实现
1.数据库导入,产生user数据表
2.用cookie功能实现
1>首页--入口页index.php
实例--首页index.php
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>cookie会话案例</title> </head> <style> body{ background-color: rgba(10,136,138,0.88); } a{ text-decoration: none; } .form{ width: 400px; height: 500px; color: #0C0C0C; box-shadow: 3px 3px 3px #2E2D3C; background-color: white; border-radius: 10px; margin-top: 80px; margin-left: 60px; padding: 20px; } </style> <body > <div class="form"> <?php //为了简化程序,使用了一个中间层,请求派发器 ,类似于框架的控制器,对用户的请求进行集中的处理. //这个首页的设计职责为:如果是已经登录的客户,则显示出登录客户的信息,显示退出按钮. //如果是未登录的客户,则直接跳转到登录页面login.php //1.如果cookie存在则显示登录信息 if(isset($_COOKIE['username']) && $_COOKIE['username']=='admin'){ echo'用户:'.$_COOKIE['username'].'已登录<br>'; echo '<a href="dispatch.php?action=logout">退出</a>'; } else { //否则,直接转到登录页面 echo '<h2><a href="dispatch.php?action=login">请登录</a></h2>'; } ?> </div> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例
2>请求分发器页面
实例
<?php //它叫请求派发器,请求分发器,前端控制器等等 //它的功能就是获取到客户的需求,并调用不同的脚本进行处理和相应 //链接数据库 require __DIR__ . '/inc/connect-yan.php'; //获取请求参数 $action = isset($_GET['action']) ? $_GET['action'] : 'login'; $action = htmlentities(strtolower(trim($action))); //请求分发,分几种情况: switch ($action){ case 'login': //加载登录表单 include __DIR__.'/login.php'; break; case 'check': //加载验证表单 include __DIR__.'/check.php'; break; case 'logout': //加载退出登录表单 include __DIR__.'/logout.php'; break; default: //header方式链接到index.php页面 // header('Location:index.php'); echo '<script>location.assign("index.php");</script>'; break; }
运行实例 »
点击 "运行实例" 按钮查看在线实例
3>登录页面--login.php
实例--登录页面login.php
<?php // 防止用户重复登录 if (isset($_COOKIE['username']) && $_COOKIE['username'] === 'admin') { echo '<script>alert("不要重复登录");location.assign("index.php");</script>'; } ?> <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>用户登录</title> </head> <style> body{ background-color: rgba(10,136,138,0.88); } a{ text-decoration: none; } .form{ width: 400px; height: 500px; color: #0C0C0C; box-shadow: 3px 3px 3px #2E2D3C; background-color: white; border-radius: 10px; margin-top: 80px; margin-left: 60px; padding: 20px; } </style> <body> <div class="form"> <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> </div> <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
实例--check.php
<?php //判断请求类型是否为post if ($_SERVER['REQUEST_METHOD'] === 'POST') { //2获取表单的数据 $email = $_POST['email']; $password= sha1($_POST['password']); // 用用户表user.dbf进行验证 $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); //判断验证的结果知否正确 if(false===$user){ //验证失败,返回到上一页 echo '<script>alert("验证失败");history.back();</script>'; die; } //验证成功 //将用户的信息写在cookie中 setcookie('username',$user['username']); echo '<script>alert("登录成功");location.assign("index.php");</script>'; exit; }else{ die('请求类型错误'); }
运行实例 »
点击 "运行实例" 按钮查看在线实例
5>退出页面logout.php
实例--退出页面logout.php
<?php //退出是要求必须在用户登录以后的退出 if (isset($_COOKIE['username']) && $_COOKIE['username'] === 'admin'){ setcookie('username',null,time()-3600); echo '<script>alert("退出成功");location.assign("index.php");</script>'; }
运行实例 »
点击 "运行实例" 按钮查看在线实例
2.session会话跟cookies相似,就是把所有的cookie改成session,同时打开session
代码入下:
实例-index.php
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>cookie会话案例</title> </head> <style> body{ background-color: rgba(10,136,138,0.88); } a{ text-decoration: none; } .form{ width: 400px; height: 500px; color: #0C0C0C; box-shadow: 3px 3px 3px #2E2D3C; background-color: white; border-radius: 10px; margin-top: 80px; margin-left: 60px; padding: 20px; } </style> <body > <div class="form"> <?php session_start(); //为了简化程序,使用了一个中间层,请求派发器 ,类似于框架的控制器,对用户的请求进行集中的处理. //这个首页的设计职责为:如果是已经登录的客户,则显示出登录客户的信息,显示退出按钮. //如果是未登录的客户,则直接跳转到登录页面login.php //1.如果cookie存在则显示登录信息 if(isset($_SESSION['username']) && $_SESSION['username']=='admin'){ echo'用户:'.$_SESSION['username'].'已登录<br>'; echo '<a href="dispatch.php?action=logout">退出</a>'; } else { //否则,直接转到登录页面 echo '<h2><a href="dispatch.php?action=login">请登录</a></h2>'; } ?> </div> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例--login.php
<?php session_start(); // 防止用户重复登录 if (isset($_SESSION['username']) && $_SESSION['username'] === 'admin') { echo '<script>alert("不要重复登录");location.assign("index.php");</script>'; } ?> <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>用户登录</title> </head> <style> body{ background-color: rgba(10,136,138,0.88); } a{ text-decoration: none; } .form{ width: 400px; height: 500px; color: #0C0C0C; box-shadow: 3px 3px 3px #2E2D3C; background-color: white; border-radius: 10px; margin-top: 80px; margin-left: 60px; padding: 20px; } </style> <body> <div class="form"> <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> </div> <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>
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例--dispatch.php
<?php //它叫请求派发器,请求分发器,前端控制器等等 //它的功能就是获取到客户的需求,并调用不同的脚本进行处理和相应 //链接数据库 require __DIR__ . '/inc/connect-yan.php'; //获取请求参数 $action = isset($_GET['action']) ? $_GET['action'] : 'login'; $action = htmlentities(strtolower(trim($action))); //请求分发,分几种情况: switch ($action){ case 'login': //加载登录表单 include __DIR__.'/login.php'; break; case 'check': //加载验证表单 include __DIR__.'/check.php'; break; case 'logout': //加载退出登录表单 include __DIR__.'/logout.php'; break; default: //header方式链接到index.php页面 // header('Location:index.php'); echo '<script>location.assign("index.php");</script>'; break; }
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例--check.php
<?php session_start(); //判断请求类型是否为post if ($_SERVER['REQUEST_METHOD'] === 'POST') { //2获取表单的数据 $email = $_POST['email']; $password= sha1($_POST['password']); // 用用户表user.dbf进行验证 $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); //判断验证的结果知否正确 if(false===$user){ //验证失败,返回到上一页 echo '<script>alert("验证失败");history.back();</script>'; die; } //验证成功 //将用户的信息写在cookie中 session_start(); $_SESSION['username'] = $user['username']; echo '<script>alert("登录成功");location.assign("index.php");</script>'; exit; }else{ die('请求类型错误'); }
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例--logout.php
<?php //退出是要求必须在用户登录以后的退出 session_start(); if (isset($_SESSION['username']) && $_SESSION['username'] === 'admin'){ session_unset(); // setcookie('username',null,time()-3600); echo '<script>alert("退出成功");location.assign("index.php");</script>'; }
运行实例 »
点击 "运行实例" 按钮查看在线实例