Correction status:qualified
Teacher's comments:业务逻辑是正确的, 如果让你自己写, 能否写出来呢?
<?php //cookie //if (isset($_COOKIE['username']) && $_COOKIE['username'] === 'admin') { // echo '用户: ' . $_COOKIE['username'] . '已登录<br>'; // echo '<a href="dispatch.php?action=logout">退出</a>'; //} else { // // 未登陆跳到登陆页面 // echo '<a href="dispatch.php?action=login">请登录</a>'; //} //session part session_start(); if (isset($_SESSION['username']) && $_SESSION['username'] === 'admin') { echo '用户: ' . $_SESSION['username'] . '已登录<br>'; echo '<a href="dispatch.php?action=logout">退出</a>'; } else { //未登陆跳到登陆页面 echo '<a href="dispatch.php?action=login">请登录</a>'; }
点击 "运行实例" 按钮查看在线实例
<?php // 请求派发器: 前端控制器 // 功能就是获取到用户的请求, 并调用不同的脚本进行处理和响应 require __DIR__ . '/db/connect.php'; //判断请求的值 $action = isset($_GET[ 'action' ]) ? $_GET[ 'action' ] : 'login'; // htmlentites ->转换成html //strtolower-> 转换在小写 //trim-> 删除空格 $action = htmlentities(strtolower(trim($action))); //分发请求 switch ($action) { //获取参数login -> 跳到login页面 case 'login': include __DIR__ . '/login.php'; break; //获取参数check -> 跳到check页面 case 'check': include __DIR__ . '/check.php'; break; //获取参数logout -> 跳到logout页面 case 'logout': include __DIR__ . '/logout.php'; break; default: header('Location : index.php'); // JS : echo '<script>location.assign("index.php");</script>'; }
点击 "运行实例" 按钮查看在线实例
<?php //cookie 部分 //if (isset($_COOKIE['username']) && $_COOKIE['username'] === 'admin') { // echo '<script>alert("你已登陆");location.assign("index.php");</script>'; //} //session 部分 session_start();//开启session 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> <body> <h3>用户登录</h3> <!--action=check: 跳转登录时进行验证--> <!--return isEmpty(): 利用function获取状态 true:成功 / false:失败--> <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 emailValue = document.querySelector('#email').value; var passwordValue = document.querySelector('#password').value; if(emailValue.length === 0 || passwordValue.length === 0){ alert('邮箱或者密码没有输入!'); //返回值为false 提交事件失敗 return false; } } </script> </body> </html>
点击 "运行实例" 按钮查看在线实例
<?php session_start(); //判断请求类型 if ( $_SERVER[ 'REQUEST_METHOD' ] === 'POST' ) { $email = $_POST[ 'email' ]; //sha1 加密方法,也可用md5 $password = sha1($_POST[ 'password' ]); //与database验证 $sql = 'SELECT * FROM `user` WHERE `email`=:email AND `password`=:password LIMIT 1'; $preObj = $pdo->prepare($sql); //执行 -> 从$_POST获取的值,在SQL中查询 $preObj->execute([ 'email' => $email , 'password' => $password ]); //返回数据 $user = $preObj->fetch(PDO::FETCH_ASSOC); if ( $user === false ) { //database验证失败 返回上一页 echo '<script>alert("验证失败!");history.back();</script>'; exit(); } //cookie用法 // setcookie('username' , $user[ 'username' ]); //Session用法 $_SESSION['username'] = $user['username']; echo '<script>alert("登陆成功");location.assign("index.php");</script>'; exit; } else { die('请求失败'); }
点击 "运行实例" 按钮查看在线实例
<?php // 必须在用户已经登录的情况下再退出 //cookie有值 = 已登入 //cookie 部分 //if ( isset($_COOKIE[ 'username' ]) && $_COOKIE[ 'username' ] === 'admin' ) { // //清空cookie -> logout // setcookie('username' , null , time() - 3600); 也可自行设置时间不一定要3600 S // echo '<script>alert("退出成功");location.assign("index.php");</script>'; //} else { // echo '<script>alert("请登录");location.assign("login.php");</script>'; //} //session 部分 session_start(); if ( isset($_SESSION[ 'username' ]) && $_SESSION[ 'username' ] === 'admin' ) { //unset session => logout session_unset(); setcookie('PHPSESSID' , null , time() - 3600); echo '<script>alert("退出成功");location.assign("index.php");</script>'; } else { // 要求用户登录 echo '<script>alert("请登录");location.assign("login.php");</script>'; }
点击 "运行实例" 按钮查看在线实例